循环语句常用于重复执行一条命令或一组命令等,直到达到结束条件后,则终止执行。在Shell中常见的循环命令有while、until、for和select等。
while语句
基础语法
while <条件表达式> do   语句 donewhile循环读取文件
- 1、使用exec
exec < FILE while read line do   command done- 2、使用cat和管道
cat FILEPATH/FILE | while read line do   command done- 3、在done后使用重定向
while read line do   command done < FILEwhile示例
1、打印数字
[root@localhost Test]# cat while.sh #!/bin/bash a=$1 while [ ${a} -ge 0 ] do   echo "Current number is:" ${a}   a=$((a-1)) done  [root@localhost Test]# bash while.sh 5 Current number is: 5 Current number is: 4 Current number is: 3 Current number is: 2 Current number is: 1 Current number is: 02、读取文件
# 读取网卡配置文件 [root@localhost Test]# cat readnet.sh #!/bin/bash while read line  do   echo ${line}  done < /etc/sysconfig/network-scripts/ifcfg-ens5f1  [root@localhost Test]# bash readnet.sh TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens5f1 UUID=dbab37df-749f-4cf5-b0a9-c9d7e6632f44 DEVICE=ens5f1 ONBOOT=yes IPADDR=192.168.8.8 NETMASK=255.255.255.0 GATEWAY=192.168.8.1until语句
基础语法
until  <条件表达式> do   语句 doneuntil语句的语法与while相似,区别在until会在条件表达式不成立时,进入循环执行命令,条件表达式成立时,终止循环。until的应用场景比较省,了解即可。
until示例
                        
                        
                    