$RANDOM获得随机数
#!/bin/bash
n=$[$RANDOM%100]
while :
do
read -p "Please input a number:" n1
n2=`echo $n1 | sed 's/[0-9]//g'`
if [ ! -z "$n2" ];then
echo "You should input the number!"
continue
fi
if [ $n1 -eq $n ];then
echo "Bingo"
break
elif [ $n1 -gt $n ];then
echo "Bigger"
continue
elif [ $n1 -lt $n ];then
echo "Small"
continue
fi
done
解释:判断n2的值是否为空:
n2=`echo $n1 | sed 's/[0-9]//g'`
if [ ! -z "$n2" ];then
如果用-n判断时需要对"$n2"加上双引号,源码里对-n和-z判断解释是:
-z的判断是通过判断字符串首位是否为\0来完成,而-n则变成了0