原本書(shū)上這個(gè)例子是教調(diào)試腳本的,它故意給出的腳本中有幾個(gè)錯(cuò)誤,教我們認(rèn)識(shí)系統(tǒng)提示的錯(cuò)誤,并修改運(yùn)行。但比較難寫(xiě)出來(lái),所以把修改好了的腳本放上來(lái),這個(gè)腳本比較有趣味的
#!/bin/sh
# hilow -- A simple number-guessing game
biggest=100 # maximum number possible
guess=0 # guessed by player
guesses=0 # number of guesses made
number=$(($$ % $biggest)) # random number, between 1 and $biggest
while [ $guess -ne $number ]
do
echo -n "Guess? " ; read guess
if [ "$guess" -lt $number ] ; then
echo "... bigger!"
elif [ "$guess" -gt $number ] ; then
echo "... smaller!i"
fi
guesses=$(($guesses + 1))
done
echo "Right!! Guessed $number in $guesses guesses."
exit 0
腳本分析
#1) number=$(($$ % $biggest)) $$代表執(zhí)行此shell的pid,因?yàn)槊看螆?zhí)行shell的pid并不相同, 所以number為1-100的隨機(jī)數(shù)。
#2)$guess -ne $number ,if [ "$guess" -lt $number ],elif [ "$guess" -gt $number ] ;為判斷輸入的數(shù)字與$number的比較。
#3)整個(gè)腳本并不復(fù)雜,但邏輯清晰明了,也很有意思。
您可能感興趣的文章:- shell腳本實(shí)現(xiàn)猜數(shù)游戲
- Shell實(shí)現(xiàn)猜數(shù)字游戲
- shell實(shí)現(xiàn)猜數(shù)字小游戲
- Shell腳本實(shí)現(xiàn)的猜數(shù)字小游戲
- Shell腳本實(shí)現(xiàn)猜數(shù)字游戲