Guess the number game. First let the system randomly generate a number, give the number a range (1-60), let the user enter the guessing number, the input to judge, if not meet the requirements, give high or low hint, guess the right after the number of guesses to use, and according to the history of guessing to beat the number of users have guessed, and the key content in a special color to prompt users.
The final results are as follows:
Publish code:
[[email protected] scripts]# cat 7.sh #!/bin/bashi= "$ ((random%60))" Count=0beat () {a=0 b=0 while read line do If [$line-ge $count] Then ((a++)) Else ((b++)) fi done</tmp/count.log Echo $a $ ((a+b)) |awk ' {print ($1/$2) *100 "%"} '}while Truedo read-p ' Please enter 1-60 number: num [$num-gt]&&{ echo "is not silly, did not see let you lose 1-60 of the number??? "Continue} [$num-lt 1] &&{echo" is not silly, did not see let you lose 1-60 of the number??? "Continue} expr + $num &>/dev/null [$?] -ne 0] &&{echo "Please enter a number, don't lose it" continue} let count+=1 if [$num-gt $i] then echo-e brothers, Lose "\033[41;37m Gao \033[0m", Come on! elif [$num-eq $i] Then if [$count-le 3] then echo-e "\033[43;32m Good, ${count} Times is right, defeated the whole Country ' Beat ' people, you can definitely become CEO, win white Rich beauty, embark on the pinnacle of life \033[0m "else echo" with $count times to correct, defeated the national ' Beat ' People "fi echo" $count ">>/tmp/count.log Exit 0 Else Echo-e brothers, lost "\033[44;37m low \033[0m", Come on! Fidone
Shell programming guessing number games. (Master please bypass)