shell版猜数字

发现好久不用shell之后,语法都生疏了;今天写个小游戏练练手。

#!/bin/bash

#产生一个100到999的随机数
NUM=`echo ${RANDOM}|cut -c-3`

#记下猜测的次数
count=0
while :
do

    echo -n "Type a number:"
    read num

    #判断输入数否为数字
    is_num=`echo ${num}|grep ^[0-9]*$`
    if [ -z $is_num ]
    then
        echo "Please type a number."
        continue
    fi

    count=`expr $count + 1`
    if [ $num -lt $NUM ]
    then
        echo "too small."
        continue
    elif [ $num -gt $NUM ]
    then
        echo "too big."
        continue
    else
        echo "That's right."
        echo "And you totally type $count times."
        exit 0
    fi
done
posted @ 2010-01-08 14:31  steven zhao  阅读(507)  评论(0编辑  收藏  举报