Shell编程-08-Shell中的循环语句
循环语句常用于重复执行一条命令或一组命令等,直到达到结束条件后,则终止执行。在Shell中常见的循环命令有while、until、for和select等。
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中常用的循环中断控制有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/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,或扫描下面的二维码添加关注:

Shell编程-08-Shell中的循环语句的更多相关文章
- shell脚本中select循环语句用法
shell脚本中select循环语句 1. 脚本中select的语法格式 select VAR in LIST do command1 command2 ... ... commandN done s ...
- Shell脚本的条件控制和循环语句
条件判断:if语句 语法格式: if [ expression ] then Statement(s) to be executed if expression is true fi 注意:expre ...
- shell编程系列7--shell中常用的工具find、locate、which、whereis
shell编程系列7--shell中常用的工具find.locate.which.whereis .文件查找之find命令 语法格式:find [路径] [选项] [操作] 选项 -name 根据文件 ...
- shell编程系列6--shell中的函数
shell编程系列6--shell中的函数 .函数介绍 linux shell中的函数和大多数编程语言中的函数一样 将相似的任务或者代码封装到函数中,供其他地方调用 语法格式 第一种格式 name() ...
- 【Shell编程】Shell程序设计
1.Shell简介 作为Linux灵感来源的Unix系统最初是没有图形化界面的,所有的任务都是通过命令行来实现的.因此,Unix的命令行系统得到了很大的发展,逐步成为一个功能强大的系统. Sh ...
- js中的循环语句
js中的循环语句可分为三种:1.while:2.do……while:3.for. while的语法为 while (exp) { //statements;} var a=1,b=0; whil ...
- shell编程中的循环语句
while循环直接从文件中读取 while read line do command done < filename until循环 until 条件 do command done for循环 ...
- Shell中的循环语句实例
1.for循环语句实例1.1 最基本的for循环 #!/bin/bash for x in one two three four do echo number $x done 注:" ...
- shell中的循环语句while
循环语句的结构: ------------| while 条件 | do | 需要执行的命令 | done | -----------| 例如: 1.while一直循环 2.whi ...
随机推荐
- sqlserver 分区排序之partition
例如:按照课程分组取各个课程最高成绩的记录,使用partition分区,然后按照成绩倒序排列,需要注意的是考虑到可能出现多个相同最高分,使用dense_rank来实现连续排序. 参考链接:https: ...
- thinkphp装修平台源码
每日签到微擎微赞自助授权中心站长工具(new)☜=每日新帖=微信开发手册VIP优惠活动 开启辅助访问切换到宽版 用户名 自动登录 找回密码 密码 登录 立即注册 只需一步,快速开始 首页 微鱼商业 ...
- C# fckeditor添加上传附件功能
最近在维护系统时,要把fckediotr加上上传附件功能,好久没有用fckeditor了,现在都已经改名字,不叫这个了. 修改统计器下面的fckconfig.js,方法如下: 1.把FCKConfig ...
- Iperf使用方法与参数说明
Iperf使用方法与参数说明 http://pkgs.repoforge.org/iperf/ Iperf是一个网络性能测试工具.可以测试TCP和UDP带宽质量,可以测量最大TCP带宽,具有多种参 ...
- swift 要点
swift 基本语法注意点 通常来说,编程语言教程中的第一个程序应该在屏幕上打印“Hello, world”.在 Swift 中,可以用一行代码实现 print("Hello, world! ...
- [Robot Framework] SikuliLibrary的关键字执行依赖java进程,但是上次的java进程如果没有杀掉,robot framework控制台的日志出不来,怎么办?
如果在suite的setup里面杀掉java进程:AutoItLibrary.Run | taskkill /F /IM java.exe 执行sikuli的关键字会报这样的错误: Connectio ...
- Spring IOC(一)体系结构
Spring IOC(一)体系结构 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) BeanFactory 是Spring ...
- mysql 压缩版安装
环境介绍:win2008_x64+mysql5.7.10 64位 1.将压缩包解压到d:\\mysql目录,并将mysql目录中的my-default.ini 重命名为my.ini 2.将my.in ...
- apache反向代理设置
为了方便在内网测试微信接口API <VirtualHost *:80> ServerName wx.abc.com ProxyPreserveHost on ProxyPass / htt ...
- Selenium+python入门
在 WebDriver 中, 将这些关于鼠标操作的方法封装在 ActionChains 类提供 ActionChains 类提供了鼠标操作的常用方法: perform(): 执行所有 ActionCh ...