当前位置:网站首页>Shell编程基础(第七篇:分支语句-if)
Shell编程基础(第七篇:分支语句-if)
2022-06-22 18:46:00 【叫我王员外就行】
前言
在执行流中,根据条件执行不同的语句,分支语句,在shell编程中,主要是if、case in的使用,我们一起学习一下
if语句
1、需要分号隔开
if true;then
echo hello world
fi由于true命令的退出状态码为0,所以包含在then与fi之间的语句会执行
2、无需分号隔开
if true
then
echo hello world
fi由于true与then不在同一行,所以;可以省略,两个格式习惯用哪个,就用哪个都一样
if-else语句
1、需要分号隔开
if true; then
echo hello world
else
echo what
fi由于true的退出状态码为永远为0,所以else与fi之间语句无法执行
2、无需分号隔开
if true
then
echo hello world
else
echo what
fi由于then与true不在同一行,无需使用;隔开
if-elif语句
1、需要分号隔开
if false; then
echo what
elif true; then
echo hello world
fi由于false命令的退出状态码固定为1,所以if语句中的代码不会执行,转而判断elif中的退出状态码
2、无需分号隔开
if false
then
echo what
elif true
then
echo hello world
fi由于false与then、true与then均不在同一行,所以可省略;隔开
if-elif-else语句
1、同一行需要分号隔开
if true; then
echo hello world
elif false; then
echo what
else
echo haha
fi由于true的退出状态码为0,所以if中的语句会执行,elif、else中的语句不会有机会还行
2、无需分号隔开
if true
then
echo hello world
elif false
echo what
else
echo haha
fi条件语句的特点
if true; then
echo hello world
elif false; then
echo what
else
echo haha
fi一旦有符合条件的语句执行,其它语句将不会执行
if判断规则
if true; then
echo hello world
fi注意:
1、if语句只判断退出状态码,没有其它语言中的boolean值,true也是个命令,由于true的退出状态码一直是0,所以表示条件总为真!
2、任何命令,只要有退出状态码都能放在if或者elif的后面作为条件,不过常用的一个条件命令为test
常用条件命令test
1、判断字符串相等
if test "a" = "a"; then
echo hello world
fi由于a与a相等,所以test命令的退出状态码为0,此时then与fi之间的语句会得到执行
2、判断文件存在
if test -f "hello.txt"; then
echo hello world
fi-f与"hello.text"均为传递给test命令的参数,所以需要严格执行单词分隔原则,使用空白字符隔开
3、test命令还有很多其他判断方式,有没有觉得test很繁琐???于是作者提供了test命令的简写方式
test命令的简写方式[]
1、判断字符串相等
if [ "a" = "a" ];then
echo hello world
fi由于[]为test命令的简写方式,所以[]之间的空格字符不可省略,表示分离test命令与参数
2、判断文件是否存在
if [ -f "hello.txt" ]; then
echo hello world
fi3、更多条件判断(字符串判断)
- [ string ]:判断string不为空
- [ -n string ]:判断string长度大于零
- [ -z string ]:判断string长度为零
- [ string1 = string2 ]: 判断string1和string2是否相同
- [ string1 == string2 ] :与[ string1 = string2 ]一样
- [ string1 != string2 ]:判断string1与string2是否不同
- [ string1 '>' string2 ]:按照字典顺序string1是否在string2后
- [ string1 '<' string2 ]:按照字典顺序string1排列在string2之前
当判断条件为真时,test命令的退出状态码为0
4、更多条件判断(文件判断)
- [ -a file ]:file 存在
- [ -d file ]:file 存在并且是一个目录
- [ -e file ]:如果 file 存在并且是可执行文件
- [ -f file ]:如果 file 存在并且是一个普通文件
…………还有很多
5、更多条件判断(整数判断)
- [ integer1 -eq integer2 ]:integer1等于integer2
- [ integer1 -ne integer2 ]:integer1不等于integer2
- [ integer1 -le integer2 ]:teger1小于或等于integer2
- [ integer1 -lt integer2 ]:integer1小于integer2
- [ integer1 -ge integer2 ]:integer1大于或等于integer2
- [ integer1 -gt integer2 ]:integer1大于integer2
当判断条件为真,test命令会返回退出状态码0
test命令的另一种简写方式[[]]
1、判断字符串相等
if [[ "a" = "a" ]]; then
echo hello world
fi2、判断文件是否存在
if [[ -f "hello.txt" ]]; then
echo hello world
fi3、与[]的区别,支持正则表达式
[[ string1 =~ regex ]]
regex一个正则表示式,=~是正则比较运算符
if写在单行中
if true; then echo hello world;fi使用;隔开即可
另类用法:写多个命令
if false; true; then
echo hello world
fifalse命令与true命令都写在了if的后面,此时if只会判断最后一个命令的退出状态码
条件与&&
if [ a = a ] && [ b = b ]; then
echo hello world
fi判断多个命令,当所有命令的退出状态码均为0时,才会执行then与fi中的语句
条件或||
if [ a = b ] || [ c = c ] ;then
echo hello world
fi判断多个命令,当任意一个命令的退出状态码为0时,会执行then与fi之间的语句
巧妙利用条件执行语句
$ command1 && command2
先执行command1,只有command1执行成功(退出状态码为0)后,才执行command2
$ command1 || command2
先执行command1,只有command1执行失败(退出状态码非0)后,才执行command2总结
1、shell编程中的if语句检查的不是boolean值,它只判断退出状态码
2、只要是有退出状态码的命令,包括内置命令、可执行程序、函数,都可以作为if后面的条件
3、shell编程不好学习的原因是:代码量太大了……
边栏推荐
- How should programmers look up dates
- 【深入理解TcaplusDB技术】单据受理之创建游戏区
- Matplotlib set axis scale interval
- Huffman tree (C language)
- 斐波那契查找(黄金分割)
- libcef最新下载地址-在VS2015下编译为MD-动态链接
- 【深入理解TcaplusDB技术】TcaplusDB事务管理——错误排查
- 请你描述下从浏览器上输入一个url到呈现出页面的整个过程。
- 金鱼哥RHCA回忆录:DO447管理用户和团队的访问--创建和管理Ansible Tower用户
- Classic interview question: a page from entering URL to rendering process
猜你喜欢

IDEA写jsp代码报错,但是正常运行解决

程序员应该怎么查日期

芯和半导体“射频EDA/滤波器设计平台”闪耀IMS2022

手把手教你IDEA创建SSM项目结构

【深入理解TcaplusDB知识库】部署TcaplusDB Local版常见问题

matplotlib设置坐标轴刻度间隔

web技术分享| 【高德地图】实现自定义的轨迹回放

An error is reported when idea writes JSP code, but it is solved by normal operation

#夏日挑战赛# 【FFH】从零开始的鸿蒙机器学习之旅-NLP情感分析

3个月自学自动化测试,薪资从4.5K到15K,鬼知道我经历了什么?
随机推荐
【深入理解TcaplusDB技术】如何启动TcaplusDB进程
Shell Sort
自己写了一个telnet命令
【深入理解TcaplusDB技术】TcaplusDB运维——日常巡检
Bubble sort, select sort, direct insert sort
关于放大器失真的原因你了解多少呢?
R语言数据预处理、把类型变量转化为因子变量,把数据集转化为h2o格式、数据集划分(训练集、测试集、验证集)
Matlab calling API
Zabbix学习笔记(三十七)
Redis中的Multi事务
Storage structure of graph (adjacency matrix)
An error is reported when idea writes JSP code, but it is solved by normal operation
【深入理解TcaplusDB技术】TcaplusDB机器如何初始化和上架
年中大促 | 集成无忧,超值套餐 6 折起
带超时的recv函数
如何低成本快速搭建企业知识库?
防火墙基础之安全策略和NAT(Easy IP)
北京大学|通过对比学习实现离线元强化学习的鲁棒任务表示
【深入理解TcaplusDB技术】运维平台中实现TcaplusDB事务管理
[deeply understand tcapulusdb technology] how to start tcapulusdb process