ifcmpab&> /dev/null # 消去输出结果thenecho"Files a and b are identical."elseecho"Files a and b differ."fi# 下面介绍一个非常实用的 “if-grep" 结构:# -----------------------------------ifgrep-qBashfilethenecho"File contains at least one occurrence of Bash."fiword=Linuxletter_sequence=inuifecho"$word"|grep-q"$letter_sequence"# 使用 -q 选项消去 grep 的输出结果thenecho"$letter_sequence found in $word"elseecho"$letter_sequence not found in $word"fiifCOMMAND_WHOSE_EXIT_STATUS_IS_0_UNLESS_ERROR_OCCURREDthenecho"Command succeed."elseecho"Command failed."fi
感谢 Stéphane Chazelas 提供了后两个例子。
样例 7-1. 什么才是真?
#!/bin/bash# 提示:# 如果你不确定某个表达式的布尔值,可以用 if 结构进行测试。echoecho"Testing \"0\""if [ 0 ]thenecho"0 is true."elseecho"0 is false."fi# 0 为真。echoecho"Testing \"1\""if [ 1 ]thenecho"1 is true."elseecho"1 is false."fi# 1 为真。echoecho"Testing \"-1\""if [ -1 ]thenecho"-1 is true."elseecho"-1 is false."fi# -1 为真。echoecho"Testing \"NULL\""if [ ] # NULL, 空thenecho"NULL is true."elseecho"NULL is false."fi# NULL 为假。echoecho"Testing \"xyz\""if [ xyz ] # 字符串thenecho"Random string is true."elseecho"Random string is false."fi# 随机字符串为真。echoecho"Testing \"$xyz\""if [ $xyz ] # 原意是测试 $xyz 是否为空,但是# 现在 $xyz 只是一个没有初始化的变量。thenecho"Uninitialized variable is true."elseecho"Uninitialized variable is flase."fi# 未初始化变量含有null空值,为假。echoecho"Testing \"-n \$xyz\""if [ -n"$xyz" ] # 更加准确的写法。thenecho"Uninitialized variable is true."elseecho"Uninitialized variable is false."fi# 未初始化变量为假。echoxyz=# 初始化为空。echo"Testing \"-n \$xyz\""if [ -n"$xyz" ]thenecho"Null variable is true."elseecho"Null variable is false."fi# 空变量为假。echo# 什么时候 "false" 为真?echo"Testing \"false\""if [ "false" ] # 看起来 "false" 只是一个字符串thenecho"\"false\" is true."#+ 测试结果为真。elseecho"\"false\" is false."fi# "false" 为真。echoecho"Testing \"\$false\""# 未初始化的变量。if [ "$false" ]thenecho"\"\$false\" is true."elseecho"\"\$false\" is false."fi# "$false" 为假。# 得到了我们想要的结果。# 如果测试空变量 "$true" 会有什么样的结果?echoexit0
if [ condition-true ]thencommand1command2...else# 如果测试条件为假,则执行 else 后面的代码段command3command4...fi
if [ -x"$filename" ]; then
Else if 与 elif
elif
elif 是 else if 的缩写。可以把多个 if/then 语句连到外边去,更加简洁明了。
if [ condition1 ]thencommand1command2command3elif [condition2 ]# 等价于 else ifthencommand4command5elsedefault-commandfi
if test condition-true 完全等价于 if [ condition-true ]。当语句开始执行时,左括号 [ 是作为调用 test 命令的标记,而右括号则不严格要求,但在新版本的 Bash 里,右括号必须补上。
bash$ type test
test is a shell builtin
bash$ type '['
[ is a shell builtin
bash$ type '[['
[[ is a shell keyword
bash$ type ']]'
]] is a shell keyword
bash$ type ']'
bash: type: ]: not found
如果你想在 Bash 脚本中使用 /usr/bin/test,那你必须把路径写全。
样例 7-2. test,/usr/bin/test,[] 和 /usr/bin/[ 的等价性
#!/bin/bashechoiftest-z"$1"thenecho"No command-line arguments."elseecho"First command-line argument is $1."fiechoif/usr/bin/test-z"$1"# 等价于内建命令 "test"# ^^^^^^^^^^^^^ # 指定全路径thenecho"No command-line arguments."elseecho"First command-line argument is $1."fiechoif [ -z"$1" ] # 功能和上面的代码相同。# if [ -z "$1" 理论上可行,但是 Bash 会提示缺失右括号thenecho"No command-line arguments."elseecho"First command-line argument is $1."fiechoif/usr/bin/[-z"$1"]# 功能和上面的代码相同。# if /usr/bin/[ -z "$1" # 理论上可行,但是会报错# # 已经在 Bash 3.x 版本被修复了thenecho"No command-line arguments."elseecho"First command-line argument is $1."fiechoexit0