if/then结构用来判断命令列表的退出状态码是否为0(因为在UNIX惯例, 0表示"成功"), 如果成功的话, 那么就执行接下来的一个或多个命令。
有一个专有命令[ (左中括号, 特殊字符). 这个命令与test命令等价, 并且出于效率上的考虑, 这是一个内建命令. 这个命令把它的参数作为比较表达式或者作为文件测试, 并且根据比较的结果来返回一个退出状态码(0 表示真, 1表示假)
在版本2.02的Bash中, 引入了[[ ... ]]扩展测试命令, 因为这种表现形式可能对某些语言的程序员来说更容易熟悉一些. 注意[[是一个关键字, 并不是一个命令
Bash把[[ $a -lt $b ]]看作一个单独的元素, 并且返回一个退出状态码
(( ... ))和let ...结构也能够返回退出状态码, 当它们所测试的算术表达式的结果为非零的时候, 将会返回退出状态码0. 这些算术扩展结构被用来做算术比较.
if-else结构
if [ condition-true ]
then
command 1
command 2
...
else
# 可选的(如果不需要可以省去).
# 如果原始的条件判断的结果为假, 那么在这里添加默认的代码块来执行.
command 3
command 4
...
fi
if-elif-else结构
if [ condition1 ]
then
command1
command2
command3
elif [ condition2 ]
# 与else if一样
then
command4
command5
else
default-command
fi
if/then结构嵌套的条件测试:
可以通过if/then结构来使用嵌套的条件测试. 最终的结果和上面使用&&混合比较操作符的结果是相同的.
if [ condition1 ]
then
if [ condition2 ]
then
do-something # But only if both "condition1" and "condition2" valid.
fi
fi
验证条件判断:
#!/bin/bash
echo
echo "testing \"0\""
if [ 0 ]
then
echo "0 is true."
else
echo "0 is false."
fi
echo
echo "testing \"1\""
if [ 1 ]
then
echo "1 is true."
else
echo "1 is false."
fi
echo
echo "testing \"-1\""
if [ -1 ]
then
echo "-1 is true."
else
echo "-1 is false."
fi
echo "testing \"NULL\""
if [ ]
then
echo "NULL is true."
else
echo "NULL is false."
fi
echo
echo "testing \"xyz\""
if [ xyz ]
then
echo "Random string is true."
else
echo "Random string is false."
fi
运行结果:
testing "0"
0 is true.
testing "1"
1 is true.
testing "-1"
-1 is true.
testing "NULL"
NULL is false.
testing "xyz"
Random string is true.
以上示例未实验所有待测条件,全部总结如下:
- 0 为真
- 1为真
- -1为真
- NULL为假
- 任一字符串为真
- 未定义的变量为假
- 未初始化的变量为假
- 赋值为NULL的变量为假
test命令
test命令在Bash中是内建命令, 用来测试文件类型, 或者用来比较字符串. 因此, 在Bash脚本中, test命令并不会调用外部的/usr/bin/test中的test命令, 这是sh-utils工具包中的一部分. 同样的, [也并不会调用/usr/bin/[, 这是/usr/bin/test的符号链接.
$ type test
test is a shell builtin
$ type '['
[ is a shell builtin
$ type ']'
-bash: type: ]: not found
test, /usr/bin/test, [ ], 和/usr/bin/[都是等价命令
[[ ]]结构比[ ]结构更加通用. 这是一个扩展的test命令, 是从ksh88中引进的
在[[和]]之间所有的字符都不会发生文件名扩展或者单词分割, 但是会发生参数扩展和命令替换
使用[[ ... ]]条件判断结构, 而不是[ ... ], 能够防止脚本中的许多逻辑错误. 比如, &&, ||, <, 和> 操作符能够正常存在于[[ ]]条件判断结构中, 但是如果出现在[ ]结构中的话, 会报错
在if后面也不一定非得是test命令或者是用于条件判断的中括号结构( [ ] 或 [[ ]] )
ex:"if COMMAND"结构将会返回COMMAND的退出状态码
#!/bin/bash
# if-test.sh
dir=/home/users
if cd "$dir" 2>/dev/null
then
echo "Now in dir."
else
echo "Can't change to $dir."
fi
ex:在中括号中的条件判断也不一定非得要if不可, 也可以使用列表结构.
#!/bin/bash
var1=20
var2=22
[ "$var1" -ne "$var2" ] && echo "$var1 is not equal to $var2"
home=/home/work/bbb
[ -d "$home" ] || echo "$home directory dose not exist."
执行结果:
20 is not equal to 22
/home/work/bbb directory dose not exist.
ex:算术测试需要使用(( )),退出状态将会与[ ... ]结构完全相反!
#!/bin/bash
(( 0))
echo "Exit status of (( 0 )) is $?."
(( 1 ))
echo "Exit status of (( 1 )) is $?."
(( 5>4 ))
echo "Exit status of (( 5>4 )) is $?."
(( 5>9 ))
echo "Exit status of (( 5>9 )) is $?."
(( 5-5 ))
echo "Exit status of (( 5-5 )) is $?."
(( 5/4 ))
echo "Exit status of (( 5/4 )) is $?."
(( 1/2 ))
echo "Exit status of (( 1/2 )) is $?."
(( 1/0 )) 2>/dev/null
echo "Exit status of (( 1/0 )) is $?."
exit 0
执行结果:
Exit status of (( 0 )) is 1.
Exit status of (( 1 )) is 0.
Exit status of (( 5>4 )) is 0.
Exit status of (( 5>9 )) is 1.
Exit status of (( 5-5 )) is 1.
Exit status of (( 5/4 )) is 0.
Exit status of (( 1/2 )) is 1.
Exit status of (( 1/0 )) is 1.