逐个字符地遍历随机字符串?

编程入门 行业动态 更新时间:2024-10-26 13:30:24
本文介绍了逐个字符地遍历随机字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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 ++))

更多推荐

逐个字符地遍历随机字符串?

本文发布于:2023-11-23 03:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619988.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:遍历   字符串   字符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!