shell实现猜数字的小游戏

#/*************************************************************************
#    > File Name: guessgame.sh
#    > Author: liyong
#    > Mail: [email protected]
#    > Created Time: 2017-03-22 01:57
#    > Last modified: 2017-03-22 01:57
randNum=$(date +%s%N)
let luckNum=${randNum:0-1:1}
echo $luckNum
let num=luckNum+1
while [ $luckNum -ne $num ];do
    read -p'Please input a num: ' num 
    #expr 当num为0结果非0,故不能作为数字判断的标准
   # expr $num + 0 &> /dev/null ||{ echo '请输入数字';exit 2; }
   echo $num |grep -E '^[[:digit:]]+$' &> /dev/null ||{ echo '请输入数字
';exit 2;}                                                              
    if [ $luckNum -eq $num ];then
        echo 'Congratulates';
        exit;
    elif [ $luckNum -gt $num ];then
        echo 'The num you input is smaller'
    else
        echo 'The num you input is bigger'
    fi  
done

你可能感兴趣的:(linux,shell)