逐个字符地遍历随机字符串? [英] Stepping through a random string character by character?

查看:42
本文介绍了逐个字符地遍历随机字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

致力于此:一个简单的回显随机键盘字符的游戏;然后让用户尽快匹配该字符(这尚未实现(显然)但这是目标)并保持正确匹配"的运行总数.

Working on this: A simple game to echo a random keyboard character; then have the user match that character as fast as possible (this is not implemented yet (obviously) but that's the goal) and keep a running total of the "correct matches."

#!/bin/bash
score=0
while true; do
clear
echo "$score"
character=$(</dev/urandom tr -dc '1234567890{}[]`~\/><!@#$%^&*()_+=-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' | head -c1)
echo "$character"
read -rsn1 input
if [ "$character" = "$input"]; then
  echo "[+]"
  score ++
  sleep 1
else
  echo "wrong!"
  score=0
  sleep 1
fi
done

直接,我的问题是:为什么我得到

Directly, my question is: Why am I getting

[: missing `]'

错误.我已经尝试了

if [ "$character" = "$input" ]; then

和第二个问题:我将如何正确地问我的下一个问题:给定一个长随机字符串,如何分解"该字符串,使其在每个字符之间都有一个空格?我没有经验/词汇来描述这个操作.

and second question: How would I properly ask my next question which is: given a long random string, how would one "explode" that string such that it has a space in between every character? I do not have the experience/lexicon to describe this operation.

非常感谢!

推荐答案

] 前必须有一个空格,像这样:

There must be a space before the ] like this:

if [ "$character" = "$input" ]; then

此外,要将 bash 置于算术模式,请替换此行:

Also, to put bash in arithmetic mode, replace this line:

score ++

与:

((score ++))

这篇关于逐个字符地遍历随机字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆