简体   繁体   English

Linux Bash编号范围回显语法错误

[英]Linux Bash Number Range echo syntax error

I am very new at programming and playing around with Linux commands to help my understanding of commands and scripts but I the script that I am creating is giving me an echo syntax error and I am scratching my head to why its giving me the error. 我在编程和使用Linux命令方面很新手,可以帮助我理解命令和脚本,但是我正在创建的脚本给了我一个echo语法错误,而我为这个错误给了我头。 The entire script is suppose to ask for a number in a range, reads keyboard input, and loops until a proper number is entered. 假设整个脚本要求输入一个范围内的数字,读取键盘输入,然后循环播放直到输入正确的数字为止。 Also tells the user they were right or wrong with each guess. 还告诉用户每次猜测是对还是错。

I do not know if the script will run correctly and what else in the script is wrong so give me feedback and show me how to fix the problem. 我不知道该脚本是否可以正确运行,以及该脚本中的其他错误是什么,因此请给我反馈并向我展示如何解决该问题。 Thanks in advance. 提前致谢。

#!/bin/bash
loop=y
for i in $(seq 10)
echo Please pick a number between 1 and 10.
Read varnumber
echo you entered: $varnumber
if 
varnumber=3
then
echo Correct number, access granted...nothing
break
if 
varnumber <2>4
then echo Incorrect number, please try again.
loop=y

Since there were multiple syntax errors, friends asked to use the shellcheck tool. 由于存在多个语法错误,因此朋友要求使用shellcheck工具。 Take a look at that tool; 看看那个工具; that will help a lot in identifying syntax errors. 这将有助于识别语法错误。

Back to your script, please change the guessNumber to any number and run this: 返回您的脚本,请将guessNumber更改为任意数字并运行以下命令:

#!/bin/bash
guessNumber=3
let "maxGuess=$guessNumber + $(( ( RANDOM % 100 )  + 1 ))"
while  [ 1 ] ; do
    printf "\n\nPlease pick a number between 1 and $maxGuess: "
    read varnumber
    echo "   you entered: $varnumber"
    if [ "$varnumber" == "$guessNumber" ] ; then
        echo "   Correct number, access granted...nothing"
        break
    else
        echo "   Incorrect number, please try again."
    fi
done

things to know: 要知道的事情:

  1. guessNumber => you configure what is the right guess. guessNumber =>您配置什么是正确的猜测。
  2. maxGuess will be calculated based on your guessNumber maxGuess将根据您的guessNumber计算
  3. $(( ( RANDOM % 100 ) + 1 )) => generates a random number between 1-100. $(( ( RANDOM % 100 ) + 1 )) =>生成一个1-100之间的随机数。 Change number after % to have larger or smaller range. 在%之后更改数字以具有更大或更小的范围。
  4. Using while loop instead of for loop, which will keep your game going until the right number is guessed. 使用while循环而不是for循环,这将使您的游戏继续进行,直到猜到正确的数字为止。 [Ctrl+c to break] [Ctrl + c中断]
  5. Key in guessNumber to get out of the loop/game. 键入guessNumber以退出循环/游戏。

Hope it give an idea how it works, happy learning bash scripting!! 希望它能给出一个工作原理,祝您学习bash脚本愉快!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM