linux的shell脚本猜数字1-100小游戏

#!/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 '你有输入正确选择,默认退出'

猜你喜欢

转载自blog.51cto.com/13956220/2170859