当前位置:网站首页>『忘了再学』Shell流程控制 — 39、特殊流程控制语句
『忘了再学』Shell流程控制 — 39、特殊流程控制语句
2022-06-23 17:23:00 【u012804784】
优质资源分享
| 学习路线指引(点击解锁) | 知识定位 | 人群定位 |
|---|---|---|
| 🧡 Python实战微信订餐小程序 🧡 | 进阶级 | 本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。 |
| Python量化交易实战 | 入门级 | 手把手带你打造一个易扩展、更安全、效率更高的量化交易系统 |
目录* 1、特殊流程控制语句介绍
1、特殊流程控制语句介绍
Shell程序或者说其他的程序,都是顺序执行的,也就是第一行执行完再执行第二行,以此类推,依次执行。
而流程控制语句,如:
if条件判断语句,你只有条件成立了,才能够执行,否则就不执行其中的程序,这就会跳过一些执行命令。for循环也是流程控制类语句,是重复执行指定次数的相同代码。
除了上面的条件判断语句和循环语句外,还有特殊流程控制语句。
比如:exit语句、break语句、continue语句。
2、exit语句
系统有一个exit命令,用于退出当前用户的登录状态。
可是在Shell脚本中,exit语句是用来退出当前脚本的。也就是说,在Shell脚本中,只要碰到了exit语句,后续的程序就不再执行,而直接退出脚本。
exit的语法如下:
shell
exit [返回值]
- 如果
exit语句之后定义了返回值,那么这个脚本执行之后的返回值就是我们自己定义的返回值。可以通过查询$?这个变量,来查看返回值。 - 如果
exit语句之后没有定义返回值,脚本执行之后的返回值是,执行exit语句之前,最后执行的一条命令的返回值。
写一个exit的例子:
需求:判断输入是否为纯数字。
shell
#!/bin/bash
# 演示exit的作用
# 接收用户的输入,并把输入赋予变量num
read -p "Please input a number:" -t 30 num
# 如果变量num的值是数字,则把num的值替换为空,否则不替换
# 把替换之后的值赋予变量y
y=$(echo $num | sed 's/[0-9]//g' )
# 说明:
# 就是通过sed命令,把变量num值中的每一个字符,只要是0-9的内容,就替换为空。
# 最后如果y的值最后为空,证明num变量中的内容全部为数字,因为全部被替换了。
# 如果y的值最后不为空,证明num变量中的内容有非数字,即num非纯数字。
# 判断变量y的值如果不为空,输出报错信息,
# 退出脚本,退出返回值为18
if [ -n "$y" ]
then
echo "Error!Please input a number!"
exit 18
# 说明:
# 如果输入的不是数字,上面这两句脚本命令会执行,
# exit语句一旦执行脚本就会终止。
else
# 如果没有退出脚本,则打印变量num中的数字
echo "The number is:$num"
执行该脚本
shell
# 给Shell脚本赋予执行权限
[[email protected] sh]# chmod 755 exit.sh
# 执行脚本
[[email protected] sh]# ./exit.sh
# 输入abc
please input num: abc
# 脚本返回请输入数字
please input number, error!!!!
# 查看$?变量,返回18,和我们脚本中设定的一样。
[[email protected] sh]# echo $?
18
# 再次执行脚本,输入123
[[email protected] sh]# ./exit.sh
please input num:123
# 脚本返回结果是输入的数字
# 证明脚本符合需求。
123
3、break语句
特殊流程控制语句break语句的作用,当程序执行到break语句时,会结束整个循环(也就是跳出这个循环,继续之后后边的命令)。
而continue语句也是结束循环的语句,不过continue语句跳过当前循环,继续执行下一次循环。
看下示意图解释下break语句:
举个例子:
先编写一个不加break语句的脚本break1.sh。
shell
[[email protected] sh]# vim sh/break1.sh
#!/bin/bash
# 输出10次变量i的值
#循环十次
for((i=1;i<=10;i=i+1))
do
# 输出变量i的值
echo $i
done
执行该脚本查看结果:
shell
[[email protected] sh]# chmod 755 break1.sh
[[email protected] sh]#./break1.sh
1
2
3
4
5
6
7
8
9
10
再编写一个加break语句的脚本break2.sh。
shell
[[email protected] ~]# vim sh/break2.sh
#!/bin/bash
# 输出10次变量i的值
# 循环十次
for((i=1;i<=10;i=i+1))
do
# 如果变量i的值等于4
if[ "$i" -eq 4 ]
then
# 退出整个循环
break
fi
# 输出变量i的值
echo $i
done
执行该脚本查看结果:
shell
[[email protected] sh]# chmod 755 break2.sh
[[email protected] sh]#./break2.sh
1
2
3
上面两个脚本的对比,可以看出当执行完
break语句后,直接跳出了整个for循环。
4、continue语句
continue语句也是结束流程控制的语句。如果在循环中,continue语句只会结束当前单次循环,
画个示意图来说明下continue语句:
举个例子:
直接编写一个加continue语句的脚本continue1.sh。
来和上边的break语句中的两个脚本进行对比。
shell
[[email protected] ~]# vim sh/continue1.sh
#!/bin/bash
# 输出10次变量i的值
# 循环十次
for((i=1;i<=10;i=i+1))
do
# 如果变量i的值等于4
if[ "$i" -eq 4 ]
then
# 退出整个循环
continue
fi
# 输出变量i的值
echo $i
done
执行该脚本查看结果:
shell
[[email protected] sh]# chmod 755 continue1.sh
[[email protected] sh]#./continue1.sh
1
2
3
5
6
7
8
9
10
从上面结果可以看出,
continue语句是把第四个循环跳过去了,然后继续执行第5次循环。这就是
continue语句和break语句的区别。
__EOF__
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vHYCaFB3-1655959167281)(https://blog.csdn.net/liuyuelinfighting)]繁华似锦的博客 - 本文链接:https://blog.csdn.net/liuyuelinfighting/p/16404434.html
边栏推荐
- 【win10 VS2019 opencv4.6 配置参考】
- Crmeb second open SMS function tutorial
- Kotlin practical skills you should know
- 科技互动沙盘是凭借什么收获人气的
- 【剑指Offer】45. 把数组排成最小的数
- Revil - blackmail Virus Emergency Response
- Paper reading (48):a Library of optimization algorithms for organizational design
- Imeta | Nannong shenqirong team released microbial network analysis and visualization R package ggclusternet
- 【故障公告】取代 memcached 的 redis 出现问题造成网站故障
- 论文阅读 (47):DTFD-MIL: Double-Tier Feature Distillation Multiple Instance Learning for Histopathology..
猜你喜欢

SimpleDateFormat在多线程环境下存在线程安全问题。

【华中科技大学】考研初试复试资料分享

暂停更新公告—行走的皮卡丘

论文阅读 (55):Dynamic Multi-Robot Task Allocation under Uncertainty and Temporal Constraints

论文阅读 (51):Integration of a Holonic Organizational Control Architecture and Multiobjective...

【ESP8266-01s】獲取天氣,城市,北京時間

Paper reading (48):a Library of optimization algorithms for organizational design

Redis Cluster

Paper reading (50):a novel matrix game with payoffs of maximal belt structure

Video anomaly detection data set (shanghaitech)
随机推荐
VNC Viewer方式的远程连接树莓派
Alien world, real presentation, how does the alien version of Pokemon go achieve?
【故障公告】取代 memcached 的 redis 出现问题造成网站故障
论文阅读 (47):DTFD-MIL: Double-Tier Feature Distillation Multiple Instance Learning for Histopathology..
Latex compiled successfully but could not be output to PDF
微信小程序startLocationUpdateBackground()简单实现骑手配送位置
客服系统搭建教程_宝塔面板下安装使用方式_可对接公众号_支持APP/h5多租户运营...
Redis cluster
2022年在网上办理股票开户安全吗?
torch学习(一):环境配置
Wiley- Open Science Joint Symposium of the documentation and information center of the Chinese Academy of Sciences, lecture 2: open access journal selection and paper submission
JS array to remove duplicate data
Self taught programming introduction, what language to learn first?
【ESP8266-01s】獲取天氣,城市,北京時間
How to make towel washing label
Goframe framework: add tracing Middleware
The draganddrop framework, a new member of jetpack, greatly simplifies the development of drag and drop gestures!
This time, thoroughly understand the SparseArray implementation principle
Reinforcement learning series (I) -- basic concepts
对抗攻击与防御 (2):对抗样本的反制策略

