Shell script to realize the number guessing game

Shell script to realize the number guessing game

Create script

# vim /root/caishuzi.sh

Scripting

#/bin/bash
sj_num=$(echo `expr $RANDOM % 100`)
count=0
while :
do
        read -p "请输入一个随机数:" num
        let count++
        echo "你输入的随机数是:$num"
        if [ $num -gt ${
    
    sj_num} ];then
                echo "你猜大了"
        elif [ $num -lt ${
    
    sj_num} ];then
                echo "你猜小了"
        else
                echo "恭喜你!你猜对了"
                echo "你总共猜了$count次"
                break
        fi
done

Run script

# sh /root/caishuzi.sh

Guess you like

Origin blog.csdn.net/qq_46023525/article/details/106817345