while循环使您可以重复执行一组命令,直到出现某些条件。它通常在需要重复操作变量的值时使用。

While - 语法

while command
do
   Statement(s) to be executed if command is true
done

这里计算Shell command,如果输出值为true,则执行给定的语句。如果command为false,则不会执行任何语句,程序将跳到DONE语句后的下一行。

While - 示例

下面是一个简单的示例,它使用While循环显示数字0到9-

#!/bin/sh

a=0

while [ $a -lt 10 ]
do
   echo $a
   a=`expr $a + 1`
done

执行后,您将收到以下输出-

0
1
2
3
4
5
6
7
8
9

参考链接

https://www.learnfk.com/linux/linux-while-loop.html