问题描述
编写一个简单的游戏脚本来猜出 42 的正确数字。
我在 for/else 语句中得到一个无限循环。有人可以告诉我为什么吗?我对 Bash 非常陌生
谢谢
#!/bin/bash
read -p "Please pick a number between 1 and 100: " x
q=exit
while true
do
if [ "$x" = "q" ]
then
exit
fi
#done
if [ "$x" -gt "42" ]; then
echo -e "That number is too high!"
else read -p "try again :"
fi
if [ "$x" -lt "42" ];
then echo -e "That number is too low!"
else read -p "try again :"
fi
if [ "$x" = "42" ] ;
then echo -e "You are Correct!!!"
else read -p "try again :"
fi
done
exit
解决方法
此行填充 $x
:
read -p "Please pick a number between 1 and 100: " x
此行填充 $REPLY
:
read -p "try again :"