发现好久不用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

相关文章:

  • 2022-12-23
  • 2021-06-06
  • 2021-09-07
  • 2021-09-18
  • 2022-01-29
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2021-09-07
  • 2021-07-27
  • 2021-08-20
  • 2021-04-25
相关资源
相似解决方案