我的英语很不好,但是我试图解释我的问题。 我想在bash上创建一个小程序,用户必须在其中猜测数字。但是我的程序没有止境。
我是bash脚本的新手,但是我每天都在努力变得更好。 我正在使用CentOS7。我转到/ 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 "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
我需要做什么,请帮忙。我知道非业余爱好者不喜欢新手,但我需要您的帮助,告诉我我的错误,我会变得更好。谢谢!
答案 0 :(得分:1)
#!/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}"
答案 1 :(得分:0)
您致电
read -p "Enter your number: " variant
在while
循环之前。
在循环内,您永远不会更改variant
,因此原始值将在while
条件下重新使用。永恒。
您可以从variant=-1
开始,然后将read
移入循环内,或执行类似@xiawi的操作。