`

shell的while循环

 
阅读更多
#!/bin/sh
#filename:2.sh
b=9
e=15
tmpb=$b
while [ $tmpb -le $e ]
do
  echo $tmpb
  #tmpb=`expr $tmpb + 1`  //ok
  tmpb=$(expr $tmpb + 1)
done


执行这个脚本后,输出的结果为:

9
10
11
12
13
14
15



注意:   第9行的功能和第10行的功能是一样的。



下面的例子是使用一个循环,必须输入一个数字才能退出.


#!/bin/sh
#filename:b

while :
do
  echo -n "input a number:"
  read Val
  expr $Val + 1 > /dev/null 2>
  res=$?
  if [ "$Val" = "" ] || [ "$res" != "0" ]; then
     echo "what you input is not a number!"
  else
    echo "what you input is : $Val"
     break;
    fi
  done


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics