Shell 流程控制语句-while

无限循环语句,满足条件,则无限匹配执行。

固定循环

语法

while [条件表达式]       => 如果怎么样(满足条件)
do                     => 那就无限次执行什么命令(动作)
done                   => 结束(固定格式)

示例

使用脚本计算1+2+3+4+......100的数值是多少。

[root@localhost ~]# vi a.sh
#!/bin/bash
a=0     
i=0
while [ $i -lt 100 ]           # 判断变量$i的值小于100(手动限定条件)
do                             # 每次循环$i加1,$a计算得出结果并显示结果
i=`expr $i + 1`;
a=`expr $a + $i`;
echo $a 
done
[root@localhost ~]# sh a.sh
1
3
6
10
15
21
省略.......
5050

数值的循环

语法

while :               => 无论如何都要执行
do                    => 要执行什么操作
done                  => 结尾(固定标识)

猜你喜欢

转载自www.cnblogs.com/network-ren/p/12515843.html
今日推荐