Use a shell script to write a guess the price of the game

[root@localhost 3]# vim game.sh 
#!/bin/bash
a=$(expr $RANDOM % 1000)
b=0
echo "商品的价格范围为0-999,猜猜看是多少?"
while true 
do
        read -p "请输入你猜测的价格:" c
        let b++
        if [ $c -eq $a ] ; then
                echo "恭喜你答对了,实际价格是 $a "
                echo "你总共猜了 $b 次"
                exit 0
        elif [ $c -gt $a ] ; then
                echo "太高了!"
        else
                echo "太低了!"
        fi
done

After the completion of the preparation we give execute permission to test

[root@localhost 3 ]# ./game.sh 
商品的价格范围为0-999,猜猜看是多少?
请输入你猜测的价格:555
太高了!
请输入你猜测的价格:444
太低了!
请输入你猜测的价格:450
太低了!
请输入你猜测的价格:460
太低了!
请输入你猜测的价格:480
太低了!
请输入你猜测的价格:490
太低了!
请输入你猜测的价格:520
太高了!
请输入你猜测的价格:510
太高了!
请输入你猜测的价格:500
太低了!
请输入你猜测的价格:505
太低了!
请输入你猜测的价格:506
太低了!
请输入你猜测的价格:507
恭喜你答对了,实际价格是 507 
你总共猜了 12 次

Guess you like

Origin blog.51cto.com/14227204/2429092