循环语句常用于重复执行一条命令或一组命令等,直到达到结束条件后,则终止执行。在Shell中常见的循环命令有while、until、for和select等。
while语句
基础语法
while <条件表达式>do 语句 done
while循环读取文件
1、使用exec
exec < FILEwhile read linedo command done
2、使用cat和管道
cat FILEPATH/FILE | while read linedo command done
3、在done后使用重定向
while read linedo command done < FILE
while示例
1、打印数字
[root@localhost Test]# cat while.sh#!/bin/basha=$1while [ ${a} -ge 0 ]do
echo "Current number is:" ${a}
a=$((a-1))
done
[root@localhost Test]# bash while.sh 5Current number is: 5Current number is: 4Current number is: 3Current number is: 2Current number is: 1Current number is: 02、读取文件
# 读取网卡配置文件[root@localhost Test]# cat readnet.sh#!/bin/bashwhile read line do
echo ${line}
done < /etc/sysconfig/network-scripts/ifcfg-ens5f1
[root@localhost Test]# bash readnet.shTYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=staticDEFROUTE=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.8NETMASK=255.255.255.0GATEWAY=192.168.8.1until语句
基础语法
until <条件表达式>do 语句 done
until语句的语法与while相似,区别在until会在条件表达式不成立时,进入循环执行命令,条件表达式成立时,终止循环。until的应用场景比较省,了解即可。
until示例
[root@localhost Test]# cat until.sh#!/bin/basha=$1until [ ${a} -ge 10 ]do
echo "Current number is:" ${a}
a=$((a-1)) if [ $a -lt 0 ] then
break
fidone[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 listdo 语句 done
在该结构中in list可以省略。在省略时,相当于in "$@" 即等价于for var in "$@"
2、第二种格式
for((ex1;exp2;exp3))do 语句 done
这种格式是类C的风格,大家也见得较多
for示例
1、打印数据
[root@localhost Test]# cat for.sh#!/bin/bashecho "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.shfirst format for sentense12345second format for sentense123452、打印文件名
[root@localhost Test]# cat printfilename.sh #!/bin/bashpath=$1for filename in $(ls $1)do
echo ${filename}
done
[root@localhost Test]# bash printfilename.sh "/root/Test/"caseif.shcase.sh
compareNum.sh
eval.sh
exec.shfor.shif.sh
para.sh
ping.sh
printfilename.sh
readnet.sh
shift.sh
testPID.sh
testposition.sh
until.shwhile.shselect语句
select 语句常用于创建选择性菜单。在执行带有select循环语句的脚本时,输出会按照数字顺序列表显示列表选项,并显示提示符(默认为#?),等待用户做出选择。
基础语法
select var in listdo 语句 done
1、在该结构中in list可以省略,省略相当于in "$@"即等价于select var in "$@"
2、select与for循环不同的是:select循环执行后会出现菜单选项等待用户选择,不会自动循环所有变量列表,而用户输入的只能是菜单项前面的数字序号,每输入一次对应的序号则会执行循环一次,直至变量后面对应的列表选取完毕
select示例
1、选择目录文件
[root@localhost Test]# cat select.sh
#!/bin/bashselect file in $(ls $1)do
echo "Current file is:"${file}
done
[root@localhost Test]# bash select.sh /root/Test1) caseif.sh 7) if.sh 13) shift.sh2) case.sh 8) para.sh 14) testPID.sh3) compareNum.sh 9) ping.sh 15) testposition.sh4) eval.sh 10) printfilename.sh 16) until.sh5) exec.sh 11) readnet.sh 17) while.sh6) for.sh 12) select.sh
#? 2Current file is:case.sh
#? 3Current file is:compareNum.sh
#? 19Current file is:
#?循环中断控制
大家有过编程基础的童鞋都知道,在循环体出现某一种,我们可以提前中断循环体。在Shell中常用的循环中断控制有break、continue、exit、return。
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/bashfor(( 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 512break test2、continue示例
[root@localhost Test]# cat continue.sh#!/bin/bashfor(( 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 512contiunue test43、exit示例
[root@localhost Test]# cat exit.sh#!/bin/bashfor(( 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 512exit test
[root@localhost Test]# echo $?88循环语句总结
1、while循环语句常用于执行守护进程以及实现我们希望循环持续执行不退出的应用,其他的循环则可以使用for和定时任务crond代替
2、根据使用频次,if和for使用最高,其次是while
共同學習,寫下你的評論
評論加載中...
作者其他優質文章