linux的shell腳本猜數字1-100小遊戲
阿新 • • 發佈:2018-09-05
ech lin cas else lse 退出 tin || 默認
#!/bin/bash
echo ‘可以輸入q或者quit退出‘
a=$[RANDOM%100+1]
while :; do
read -p ‘請輸入一個數字(1-100):‘ num
i=`echo $num | sed ‘s/[0-9]//g‘`
if [ -z "$num" ];then
echo ‘不能什麽都不輸入‘
continue
fi
if [ $num == q ] || [ $num == quit ];then
exit 2
fi
if [ ! -z "$i" ];then
echo ‘你輸入的不是數字‘
continue
fi
if [ $num -lt 1 ] || [ $num -gt 100 ];then
echo ‘你輸入的數字不再1-100內‘
continue
fi
if [ $num -lt $a ];then
echo ‘猜錯了,太小了‘
elif [ $num -gt $a ];then
echo ‘猜錯了,太大了‘
else
echo ‘恭喜你,猜對了‘
read -p ‘還想再來一局嗎,請輸入yes或者no:‘ ab
case $ab in
yes)
continue
;;
no)
exit
;;
*)
break
;;
esac
fi
done
echo ‘你有輸入正確選擇,默認退出‘
linux的shell腳本猜數字1-100小遊戲