PDF版 ePub版

# 循环

• while 循环
• for 循环
• until 循环
• select 循环

## 语法

    while command1 ; # this is loop1, the outer loop
do
Statement(s) to be executed if command1 is true

while command2 ; # this is loop2, the inner loop
do
Statement(s) to be executed if command2 is true
done

Statement(s) to be executed if command1 is true
done

## 例子

    #!/bin/sh

a=0
while [ "$a" -lt 10 ]# this is loop1 do b="$a"
while [ "$b" -ge 0 ] # this is loop2 do echo -n "$b "
b=expr $b - 1 done echo a=expr$a + 1
done

    0
1 0
2 1 0
3 2 1 0
4 3 2 1 0
5 4 3 2 1 0
6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0