Shell编程基础(第7篇:分支语句-if)

前言

在执行流中,根据条件执行不同的语句,分支语句,在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
fi

3、更多条件判断(字符串判断)

  • [ 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
fi

2、判断文件是否存在

if [[ -f "hello.txt" ]]; then
    echo hello world
fi

 3、与[]的区别,支持正则表达式

[[ string1 =~ regex ]]

regex一个正则表示式,=~是正则比较运算符

if写在单行中

if true; then echo hello world;fi

使用;隔开即可

另类用法:写多个命令

if false; true; then
    echo hello world
fi

false命令与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编程不好学习的原因是:代码量太大了……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值