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

匿名 (未验证) 提交于 2019-12-02 22:59:29

whileuntilforselect等。

while <条件表达式> do   语句 done
  • 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

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  <条件表达式> do   语句 done

until语句的语法与while相似,区别在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]#

1、第一种格式

for var in list do   语句 done

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

2、第二种格式

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

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

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 var in list do   语句 done

1、在该结构中in list可以省略,省略相当于in "$@"即等价于select var in "$@"
2、select与for循环不同的是: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: #?

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,或扫描下面的二维码添加关注:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!