您的位置:首页 > 运维架构 > Shell

在shell中使用while循环的例子

2008-12-03 11:44 387 查看
#!/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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: