until命令和while命令工作的方式完全相反。until命令要求你指定一个通常返回非零退出状态码的测试命令。只要测试命令的状态码不为0,bash shell才回执行循环中列出的命令。一旦测试命令返回了状态码0,循环就结束了。
和你想的一样,until命令的格式如下:
until test commands
do
other commands
done
和while命令类似,你可以在until命令语句中放入多个测试命令。只有最后一个命令状态退出码决定了bash shell是否执行已定义的other commands.
下面是使用until命令的一个例子。
#! /bin/bash
# using the until command
var1=100
until [ $var1 -eq 0 ]
do
echo $var1
var1=$[ $var1 - 25 ]
done
~
[root@ecs robin]# ./until01.sh
100
75
50
25
本例中会测试var1变量来决定until循环何时停止。只要改变量的值等于0,until命令就会停止循环。同whil命令一样,在until命令中使用多个测试命令时要注意。
#! /bin/bash
# using the until command
var1=100
until echo $var1
[ $var1 -eq 0 ]
do
echo inside the loop: $var1
var1=$[ $var1 - 25 ]
done
[root@ecs robin]# ./until02.sh
100
inside the loop: 100
75
inside the loop: 75
50
inside the loop: 50
25
inside the loop: 25
0
shell会执行指定的多个命令,只有在最后一个命令成立时停止。
1092

被折叠的 条评论
为什么被折叠?



