#!/bin/bash
echo "这是一个猜数小游戏,可猜数字0--9"
echo -e "请输入游戏次数,\n无限次游戏请输入forever,\n无限次游戏请输入forever,\n次游戏请输入n。\nn:\c"
secrec_num=`expr $RANDOM % 10`
read game_type
if [ game_type = "forever" ]
then
echo "无限次游戏模式"
else
echo "游戏体验次数为:${game_type}"
fi
while [ $game_type = "forever" -o $game_type ]
do
if [ $game_type != "forever" ]
then
game_type=$[$game_type-1]
if [ 0 == $game_type ]
then
echo "这是最后一次游戏机会了!!!"
fi
fi
echo -e "请猜一个数,或者输入q退出游戏:\c"
read temp
if [ $temp == 'q' ]
then
break
fi
num=$temp
if [ $num == $secrec_num ]
then
echo "恭喜猜对"
break
else
echo "猜错了"
if [ $num -gt $secrec_num ]
then
if [ $game_type == 0 ]
then
echo "最后一次游戏机会用完!!!"
else
echo "猜的过大,往小的猜"
fi
else
if [ $game_type == 0 ]
then
echo "最后一次游戏机会用完!!!"
break
else
echo "猜的过小,往大的猜"
fi
fi
fi
done
echo "游戏结束"
来源:CSDN
作者:江南又旧雨
链接:https://blog.csdn.net/weixin_38293453/article/details/104363399