shell学习之路:流程控制(while)

while循环

介绍:while循环是不定循环,也称作条件循环。只要条件判断成立,循环就会一直继续执行,直到条件判断不成立,循环才会停止,这就是和for的固定循环不太一样了、

1 while [ 条件判断 ]
2     do
3         程序
4     done

示例:

 1 [root@localhostA1 bash]# vi while1.sh
 2 #!/bin/bash
 3 #从1到100
 4 i=1
 5 s=0
 6 while [ $i -le 100 ] ;do
 7         s=$(( $s+$i ))
 8         i=$(( $i+1 ))
 9         done
10 echo "The sum is :$s"
11 [root@localhostA1 bash]# chmod u+x while1.sh 
12 [root@localhostA1 bash]# ./while1.sh 
13 The sum is :5050

2.until循环:

介绍:和while循环相反,until循环时只要条件判断不成立,则执行,直到条件成立停止

 1 [root@localhostA1 bash]# vi nutil1.sh
 2 #!/bin/bash
 3 i=1
 4 s=0
 5 until [ $i -gt 100 ]
 6         do
 7                 s=$(( $s+$i ))
 8                 i=$(( $i+1 ))
 9         done
10 echo "The sum is :$s"
11 [root@localhostA1 bash]# chmod u+x nutil1.sh 
12 [root@localhostA1 bash]# ./nutil1.sh 
13 The sum is :5050

OK

原文地址:https://www.cnblogs.com/patf/p/4609287.html