Shell的If语句祥解

Shell的If语句祥解

一般写脚本我们不是只希望脚本直接的从上往下执行下去,

我们希望自己的脚本有一定的逻辑判断(规定这部分的代码要不要执行)和循环(执行几次),

Shell中的if语句结构:
 

形式1:

if 语句

if condition

then

   statement(s)

fi


形式2:

if condition;then

   statement(s)

fi

注意:condition后边的分号;当if和then位于同一行的时候,这个分号是必须的,否则会有语法错误(如果if和then不在同一行时则可以不用写分号)


 

Python或者java中 : if语句之后的条件是一个等式,

等式表示的值为True或False

                             如果是True我们就执行if肚子里面的内容,

如果是False就不会进入这个判断语句里面了

shell中的if : if语句会运行后面的condition语句(命令或者等式),

             它会根据condition命令退出状态(用到了echo $?的知识)

             (可以通过echo $? 来获取命令的退出状态码)

         # 命令退出状态码为0说明命令执行成功,then后的语句就会执行

         # 命令退出状态码不为0,说明命令执行失败,then后的语句就不会执行

             (放在脚本最后面就是返回脚本退出时的状态码,

             如果在echo $?前面调用函数,

             则执行脚本时会打印出调用函数的返回值(给用户看到))

 

[root@localhost mnt]# cat 02.sh

#!/bin/bash

if date

then

         echo "it works"

fi

 

[root@localhost mnt]# sh 02.sh

Wed Feb 12 14:27:45 CST 2020       

#执行了condition命令(date)后,condition命令执行成功

it works    

#condition命令执行成功,返回值为0,然后执行then后面的语句echo “it works”


当脚本中if后的condition错误命令dateffff执行不了的

(shell不能解释这个命令啥意思,

这个condition命令的生成的错误消息依然会在脚本中输出),

则输出一个非0的退出状态码,则不执行then后的语句

[root@localhost mnt]# cat 03.sh

#!/bin/bash

if dateffff

then

         echo "it works"

fi

echo "we are outside if the if statement"

 

[root@localhost mnt]# sh 03.sh

03.sh: line 2: dateffff: command not found

we are outside if the if statement

只要这个condition命令shell能识别,我们都可以写到if里面

 

[root@localhost mnt]# cat 04.sh

#!/bin/bash

testing=student

if grep $testing /etc/passwd

then

         echo The bash files for user $testing are:

         ls -a /home/$testing/.b*

fi

[root@localhost mnt]# sh 04.sh

student:x:1001:1001::/home/student:/bin/bash

The bash files for user student are:

/home/student/.bash_logout 

/home/student/.bash_profile 

/home/student/.bashrc

 

[root@localhost mnt]# cat 04.sh

#!/bin/bash

testing=studentggggggg

if grep $testing /etc/passwd

then

         echo The bash files for user $testing are:

         ls -a /home/$testing/.b*

fi

[root@localhost mnt]# sh 04.sh

 

后面可以跟你随便想跟的用户

if在python经常被用来判断

这里用shell来写一个比较大小的脚本

 

双引号:(())         #数学计算命令,可以加减乘除,

                         还可以进行大于,小于,等于的关系运算,

                          逻辑运算与或非的运算

 

[root@localhost mnt]# cat 05.sh

#!/bin/bash

read a

read b

if (( $a == $b ))

then

         echo "a 和b相等"

fi

[root@localhost mnt]# vim 06.sh

[root@localhost mnt]# sh 05.sh

1

4

[root@localhost mnt]# sh 05.sh

4

4

a 和b相等

 

[root@localhost mnt]# cat 06.sh

#!/bin/bash

read num1

read num2

if (( $num1 > 18 && $num2 < 60 ))

then

         echo $num1

         echo $num2

fi

[root@localhost mnt]# sh 06.sh

20

40

20

40

[root@localhost mnt]# 20

bash: 20: command not found...

[root@localhost mnt]# vim 06.sh

[root@localhost mnt]# sh 06.sh

30

30

30

30

[root@localhost mnt]# sh 06.sh    

##不满足condition命令,则不执行then中的语句

17

16

if else语句

如果有两个分支,就可以使用if else语句,它的格式为:

if condition

then

statement1

else

statement2

fi

 

如果condition成立,那么then后边的statement1语句将会被执行;

否则,执行else后边的statement2语句

 

[root@localhost mnt]# cat 05.sh

#!/bin/bash

read a

read b

if (( $a == $b ))

then

         echo "a 和b相等"

else

         echo "a 和b不相等"

fi

[root@localhost mnt]# sh 05.sh

5

5

a 和b相等

[root@localhost mnt]# sh 05.sh

9

8

a 和b不相等

if elif else 语句

Shell支持任意数目的分支,当分支比较多时,可以使用if elif else结构,它的格式为:

if condition1       #注意if和elif前都跟着then

then

statement1

elif condition2

then

statement2

elif condition3

then

statement3

……

else

statementn

fi
整条语句的逻辑:

如果condition1成立执行statement1,condition1不成立执行后面的elif

如果condition2成立执行statement2,condition1不成立执行后面的elif

如果condition3成立执行statement3,condition1不成立执行后面的elif

婴儿幼儿少年成年青年中年老年

如果所有if和elif判断都不成立,就进入最后的else,执行statementn

 

#!/bin/bash

read age

if (( $age <= 2 ));then

        echo "婴儿"

elif (( $age >= 3 && $age <= 8));then

        echo "幼儿"

elif (( $age >= 9 && $age <= 17));then

        echo "少年"

elif (( $age >= 18 && $age <= 25));then

        echo "成年"

elif (( $age >= 26 && $age <= 40));then

        echo "青年"

elif (( $age >= 41 && $age <= 60));then

        echo "中年"

else

        echo "老年"


fi

 

 

[root@localhost mnt]# sh 07.sh

2

婴儿

[root@localhost mnt]# vim 07.sh

[root@localhost mnt]# sh 07.sh

20

成年

[root@localhost mnt]# sh 07.sh

18

成年

[root@localhost mnt]# sh 07.sh

30

青年

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值