脚本目标:
- 随机生成一个介于1-1000的数字
- 输入空值或数字字符时候提示重新输入
- 提醒玩家输入整数,并根据输入值提醒大小,答对则退出
脚本内容:
#!/bin/bash typeset NUM=$[RANDOM % 1000+1] while read -p "Enter the number you guessed[1-1000]:" i; do expr $i + 1 >/dev/null 2>&1 if [ $? -ne 0 ];then echo "请输入整数" elif [ ! $i ];then echo "未检测到输入,请重新输入" elif [ $i -eq $NUM ];then echo "恭喜,答对了!" exit elif [ $i -gt $NUM ];then echo "数字太大." else echo "数字太小." fi done
运行结果:
[root@node1 script]# sh zz Enter the number you guessed[1-1000]:45jk 请输入整数 Enter the number you guessed[1-1000]:789 数字太大. Enter the number you guessed[1-1000]:10.23 请输入整数 Enter the number you guessed[1-1000]:500 数字太小. Enter the number you guessed[1-1000]:600 数字太大. Enter the number you guessed[1-1000]:550 数字太小. Enter the number you guessed[1-1000]:560 数字太大. Enter the number you guessed[1-1000]:551 恭喜,答对了! [root@node1 script]#