Linux 如何创建正常运行的程序?因为我的程序一直在运行,不会停止
我的英语很差,但我正在努力解释我的问题。 我想在bash上创建一个小程序,用户必须在其中猜测数字。但我的计划没有尽头 我是bash脚本的新手,但我每天都在努力做得更好。 我用的是CentOS 7。我转到/usr/bin/local并创建一些文件,给他chmod+x并用gedit打开:Linux 如何创建正常运行的程序?因为我的程序一直在运行,不会停止,linux,bash,shell,Linux,Bash,Shell,我的英语很差,但我正在努力解释我的问题。 我想在bash上创建一个小程序,用户必须在其中猜测数字。但我的计划没有尽头 我是bash脚本的新手,但我每天都在努力做得更好。 我用的是CentOS 7。我转到/usr/bin/local并创建一些文件,给他chmod+x并用gedit打开: #!/usr/bin/bash #This script should ask us our name and play the game "Guess the Number" with us. echo "W
#!/usr/bin/bash
#This script should ask us our name and play the game "Guess the Number" with us.
echo "What is your name?"
read name
echo "Hello $name"
echo "I want to play $ name with you, you have to guess the number from 1 to 10)"
echo "Are you ready?"
read rdy
answer=$(( answer = $RANDOM % 10 ))
read -p "Enter your number: " variant
while [ $answer != $variant ]; do
if [ $answer -gt $variant ]; then
echo "Your number is bigger, try again"
elif [ $answer -lt $variant ]; then
echo "Your number is smaller, try again"
continue
fi
done
eternal "Your number is smaller/bigger"
until you press ctrl+c
我需要做什么,请帮忙。我知道非业余爱好者不喜欢新手,但我需要你的帮助,告诉我我的错误,我会变得更好。谢谢 你打电话吗
#!/bin/bash
echo "What is your name?"
read name
echo "Hello $name"
echo "I want to play with you, you have to guess the number from 1 to 10"
answer=$(( $RANDOM % 10 ))
while true
do
read -p "Enter your number: " variant
if [ "$answer" -gt "$variant" ]; then
echo "The number is bigger, try again"
elif [ "$answer" -lt "$variant" ]; then
echo "The number is smaller, try again"
else
break;
fi
done
echo "Yes, the answer was ${answer}"
read -p "Enter your number: " variant
在while
循环之前
在循环中,您永远不会更改变量
,因此原始值将在while
条件中重新使用。永远
您可以从
variant=-1
开始,将read
移动到循环内,或执行类似@xiawi的操作。read-p“输入您的号码:”variant while[$answer!=$variant]代码>读取变量并在循环endlessy中检查它,而不是每次更改它。我猜你想读循环中的变量。另外,通过运行脚本并记住引用变量。非常感谢!我需要练习。谢谢