- number=$(( $$ % $biggest ))
$(( )) 是用于计算的
- $$ % $biggest
$$ 是当前该脚本shell的进程ID,通常是一个4位或者5位的数字,每次执行生成的进程ID都不一样,因此会生成一个随机数
- /bin/echo -n “Guess?” ;
-n表示不换行
- read guess
读取用户输入的值,赋值于guess
#!/bin/bash
biggest=100
guess=0
guesses=0
number=$(( $$ % $biggest ))
echo "猜数字在1到100之间"
echo $number
while [ "$guess" -ne $number ] ; do
/bin/echo -n "Guess?" ;
read guess
echo $guess
if [ "$guess" -lt $number ] ; then
echo "小了...."
elif [ "$guess" -gt $number ] ; then
echo "大了...."
fi
guesses=$(( $guesses + 1 ))
done
echo "对了,数字是 $number 猜了 $guesses 次."
exit 0