Shell编程-08-Shell中的循环语句


    循环语句常用于重复执行一条命令或一组命令等,直到达到结束条件后,则终止执行。在Shell中常见的循环命令有whileuntilforselect等。

while语句

基础语法

while <条件表达式>
do
  语句
done

while循环读取文件

  • 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 < FILE

while示例

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: 0

2、读取文件

# 读取网卡配置文件
[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.1

until语句

基础语法

until  <条件表达式>
do
  语句
done

until语句的语法与while相似,区别在until会在条件表达式不成立时,进入循环执行命令,条件表达式成立时,终止循环。until的应用场景比较省,了解即可。

until示例

[root@localhost Test]# cat until.sh
#!/bin/bash
a=$1
until [ ${a} -ge 10 ]
do
  echo "Current number is:" ${a}
  a=$((a-1))
  if [ $a -lt 0 ]
   then
    break
  fi
done
[root@localhost Test]# bash until.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: 0
[root@localhost Test]# bash until.sh 50 # 满足条件时,则不进入循环体
[root@localhost Test]#

for语句

    for循环语句与while循环诗句类似,但for循环语句主要用于有限次的循环场景,while主要无限次循环的场景,如守护进程

基础语法

1、第一种格式

for var in list
do
  语句
done

在该结构中in list可以省略。在省略时,相当于in "$@" 即等价于for var in "$@"

2、第二种格式

for((ex1;exp2;exp3))
do
  语句
done

这种格式是类C的风格,大家也见得较多

for示例

1、打印数据

[root@localhost Test]# cat for.sh
#!/bin/bash
echo "first format for sentense "
for i in {1..5}
do
  echo ${i}
done

echo "second format for sentense"
for((j=1;j<=5;j++))
do
 echo ${j}
done
[root@localhost Test]# bash for.sh
first format for sentense
1
2
3
4
5
second format for sentense
1
2
3
4
5

2、打印文件名

[root@localhost Test]# cat printfilename.sh 
#!/bin/bash
path=$1
for filename in $(ls $1)
do
 echo ${filename}
done
[root@localhost Test]# bash printfilename.sh "/root/Test/"
caseif.sh
case.sh
compareNum.sh
eval.sh
exec.sh
for.sh
if.sh
para.sh
ping.sh
printfilename.sh
readnet.sh
shift.sh
testPID.sh
testposition.sh
until.sh
while.sh

select语句

    select 语句常用于创建选择性菜单。在执行带有select循环语句的脚本时,输出会按照数字顺序列表显示列表选项,并显示提示符(默认为#?),等待用户做出选择。

基础语法

select var in list
do
  语句
done

1、在该结构中in list可以省略,省略相当于in "$@"即等价于select var in "$@"
2、select与for循环不同的是:select循环执行后会出现菜单选项等待用户选择,不会自动循环所有变量列表,而用户输入的只能是菜单项前面的数字序号,每输入一次对应的序号则会执行循环一次,直至变量后面对应的列表选取完毕

select示例

1、选择目录文件

[root@localhost Test]# cat select.sh 
#!/bin/bash
select file in $(ls $1)
do
 echo "Current file is:"${file}
done

[root@localhost Test]# bash select.sh /root/Test
1) caseif.sh	       7) if.sh		    13) shift.sh
2) case.sh	       8) para.sh	    14) testPID.sh
3) compareNum.sh       9) ping.sh	    15) testposition.sh
4) eval.sh	      10) printfilename.sh  16) until.sh
5) exec.sh	      11) readnet.sh	    17) while.sh
6) for.sh	      12) select.sh
#? 2
Current file is:case.sh
#? 3
Current file is:compareNum.sh
#? 19
Current file is:
#?

循环中断控制

    大家有过编程基础的童鞋都知道,在循环体出现某一种,我们可以提前中断循环体。在Shell中常用的循环中断控制有breakcontinueexitreturn

break/continue:常用于if、for、while等条件和循环语句中,从而控制流程的走向
exit:常用于终止所有语句并退出当前脚本,也可以用于返回前一次程序或命令的执行状态
return:类似于exit,但return仅适用于函数内部返回函数的执行状态值

以上详细解释如下所示:

命令 解释
break n n:跳出循环的层数;如省略n,则跳出整个循环
continu n n: 退到第n层继续循环;如省略n,则跳过本次循环,继续下一次循环
exit n 退出当前Shell进程;n:上一次程序执行的状态返回值, 如省略n,可使用$?获取执行状态值
return n 用于函数的返回值,可以用来判断函数执行是否正确

循环中断控制示例

1、break示例

[root@localhost Test]# cat break.sh
#!/bin/bash

for(( i=1;i<$1;i++ ))
do
  if [ ${i} -eq 3 ]
    then
     echo "break test"
     break
  fi
 echo ${i}
done
[root@localhost Test]# bash break.sh 5
1
2
break test

2、continue示例

[root@localhost Test]# cat continue.sh
#!/bin/bash

for(( i=1;i<$1;i++ ))
do
  if [ ${i} -eq 3 ]
    then
     echo "contiunue test"
     continue
  fi
 echo ${i}
done
[root@localhost Test]# bash continue.sh 5
1
2
contiunue test
4

3、exit示例

[root@localhost Test]# cat exit.sh

#!/bin/bash
for(( i=1;i<$1;i++ ))
do
  if [ ${i} -eq 3 ]
    then
     echo "exit test"
     exit 88
  fi
 echo ${i}
done
[root@localhost Test]# bash exit.sh 5
1
2
exit test
[root@localhost Test]# echo $?
88

循环语句总结

  • 1、while循环语句常用于执行守护进程以及实现我们希望循环持续执行不退出的应用,其他的循环则可以使用for和定时任务crond代替
  • 2、根据使用频次,if和for使用最高,其次是while

本文同步在微信订阅号上发布,如各位小伙伴们喜欢我的文章,也可以关注我的微信订阅号:woaitest,或扫描下面的二维码添加关注:
MyQRCode.jpg

posted @ 2018-11-21 00:18  Surpassme  阅读(686)  评论(0编辑  收藏  举报