while命令有点象if-then和for循环的结合。while测试命令返回0退出状态,就循环一组命令。

while基本格式:

while test command

do 

  other commands

done

示例:
#!/bin/bash
varl=5
while [ $varl -gt 0 ]
do
 echo $varl
 varl=$[ $varl -1 ]
done
[root@localhost ~]# ./test10.sh 
5
4
3
2
1
注意:因为while是检测到退出状态为0就执行,只有让输出为非0才会停止循环,
 varl=$[ $varl -1 ]得出的结果为非0


使用多条测试命令
#!/bin/bash
var1=3
while echo $var1
[ $var1 -ge 0 ]
do
 echo "The is inside the loop"
 var1=$[ $var1 -1 ]
done


[root@localhost ~]# ./test11.sh 
3
The is inside the loop
2
The is inside the loop
1
The is inside the loop
0
The is inside the loop
-1
在多行命令中,所有的测试命令都在每次失代中执行,包含测试命令失败的最后一次失代