之前讲过了linux之shell变量,光有变量还不够,对于任何脚本语言或者编程语言来说,变量、条件判断、循环等是必不可少的,shell编程也不例外。今天我就先来讲讲shell条件判断语句。
#### 条件判断if-else语句
许多时候,我们的脚本并不是简单的一路执行下去,而需要做各种条件判断。我们学过C语言或者java中的条件判断,其实shell脚本条件判断跟他们类似。例如c语言,if后面紧跟一个表达式,这个表达式的计算结果如果为True,则执行后面的语句,如果表达式结算结果为False,则不执行后面的语句。**shell的if语句有点不一样,它根据if后面命令执行后的退出状态码判断,如果退出状态码为0,执行then后面的语句,否则不执行then后面的语句。**我们在讲“Linux之shell输入输出重定向”的时候,已经讲个各种退出返回码的含义。
pwd可以成功执行,退出码为0,所以then后面的命令执行了。[root@aliyun-hk1 linux-shell-test]# cat ifelse.sh#!/bin/bashif pwdthenecho "hello world"fi[root@aliyun-hk1 linux-shell-test]# ./ifelse.sh/apps/linux-shell-testhello world[root@aliyun-hk1 linux-shell-test]#pwd后面的icantrun执行报错,退出返回码不为0,所以then后面的命令不执行[root@aliyun-hk1 linux-shell-test]# cat ifelse2.sh#!/bin/bashif icantrunthenecho "hello world"fi[root@aliyun-hk1 linux-shell-test]# ./ifelse2.sh./ifelse2.sh: line 3: icantrun: command not found[root@aliyun-hk1 linux-shell-test]#
#### 条件判断if-then-else语句
if-then语句类似于c语言中的if语句,则if-then-else相当于c语言中的if-else语句。if-then-else语句中,如果if语句后面的命令退出返回码为0,then后面的命令会执行,这跟普通的if-then一样;如果if语句后面的命令退出返回码不为0,then语句后面的命令不执行,但是执行else后面的语句。
在用户配置文件中找不到lily,退出返回码不为0[root@aliyun-hk1 linux-shell-test]# cat if-then-else.sh#!/bin/bashif cat etc/passwd|grep lilythenecho "lily can found"elseecho "lily can't found"fi[root@aliyun-hk1 linux-shell-test]# ./if-then-else.shlily can't found[root@aliyun-hk1 linux-shell-test]#
#### 条件判断if-then-elif-then语句
这个其实就是我们常说的if嵌套语句的简化写法,如果if后面的命令退出返回码不为0,不执行相邻的then语句,再如果elif后面的命令退出返回码为0,则执相邻的then语句。
[root@aliyun-hk1 linux-shell-test]# cat ifthenelifthen.sh#/bin/bashif iamnotcommandthenecho "first command return 0"elif pwdthenecho "second command return 0"fi[root@aliyun-hk1 linux-shell-test]# chmod 755 ifthenelifthen.sh[root@aliyun-hk1 linux-shell-test]# ./ifthenelifthen.sh./ifthenelifthen.sh: line 3: iamnotcommand: command not found/apps/linux-shell-testsecond command return 0[root@aliyun-hk1 linux-shell-test]#
#### 条件判断if-then-elif-then-多重嵌套语句
如果if后面的命令退出返回码不为0,不执行相邻的then语句,再如果elif后面的命令退出返回码为0,则执相邻的then语句,以此类推,只要第一次遇到命令退出返回码为0,执行相邻的then后,后面的命令都不执行了。
[root@aliyun-hk1 linux-shell-test]# cat ifthenelifthen.sh#/bin/bashif iamnotcommandthenecho "first command return 0"elif 1pwdthenecho "second command return 0"elif 2pwdthenecho "3st command retrun 0"elif pwdthenecho "4st command return 0"elif pwdthenecho "5st command return 0"fi[root@aliyun-hk1 linux-shell-test]# ./ifthenelifthen.sh./ifthenelifthen.sh: line 3: iamnotcommand: command not found./ifthenelifthen.sh: line 6: 1pwd: command not found./ifthenelifthen.sh: line 9: 2pwd: command not found/apps/linux-shell-test4st command return 0[root@aliyun-hk1 linux-shell-test]#
>注意then后面你可以使用不止一条命令,可以像在其他编程语言中一样,列出多条命令。
#### 使用test判断一个条件是否成立
我们能不能像其他编程语言一样,判断一个条件是否成立,成立的话执行一个语句,不成立的话执行另外语句?答案是肯定的。我们可以使用if语句结合test命令来实现。test command,如果这个commad成立,则这条test语句返回0,否则返回非0。test命令可以用来做数值比较、字符串比较、文件比较。
[root@aliyun-hk1 linux-shell-test]# cat iftestthen.sh这里就通过test判断两个变量的值是否相等。#!/bin/bashname1=robinname2=robinif test $name1=$name2thenecho "name1 same as name2"fi[root@aliyun-hk1 linux-shell-test]# ./ififelse2.sh ifelse.sh iftestthen.sh ifthenelifthen.sh if-then-else.sh[root@aliyun-hk1 linux-shell-test]# ./iftestthen.shname1 same as name2[root@aliyun-hk1 linux-shell-test]#
#### 使用[]判断一个条件是否成立
如果[]内的条件成立,则退出返回码为0,执行then后面的命令。
第一种方式[root@aliyun-hk1 linux-shell-test]# cat ifelse.sh#!/bin/bashage=18if [ $age -gt 18 ]thenecho "age is more than 18"elif [ $age -eq 18 ]thenecho "age is 18"fi[root@aliyun-hk1 linux-shell-test]# ./ifelse.shage is 18[root@aliyun-hk1 linux-shell-test]#第二种方式[root@aliyun-hk1 linux-shell-test]# cat ifelse.sh#!/bin/bashage=18if [ $age=18 ]thenecho "age is more than 18"elif [ $age=18 ]thenecho "age is 18"fi[root@aliyun-hk1 linux-shell-test]# ./ifelse.shage is more than 18[root@aliyun-hk1 linux-shell-test]#
>这里有人提出疑问了,定义一个整形变量不是要使用declare -i吗? 那里说的是交互式shell,今天我们已经再搞shell编程了!
#### shell编程if语句的高级用法
例如if (( $var1 ** > 90 )) 双括号允许你使用高级的数学表达式。
例如if [[ $USER == r* ]] 双方括号允许你使用高级的字符串表达式。
>shell 脚本语言对符号的使用很多,这一点让用习惯了python、java、c的同学有点不习惯。所以,我们先挑选熟悉常用的,高级复杂的需要了再学。
#### 下次一我们将会讲解shell for循环的使用。
敬请期待。




