if cmp a b &> /dev/null # 消去输出结果
then echo "Files a and b are identical."
else echo "Files a and b differ."
fi
# 下面介绍一个非常实用的 “if-grep" 结构:
# -----------------------------------
if grep -q Bash file
then echo "File contains at least one occurrence of Bash."
fi
word=Linux
letter_sequence=inu
if echo "$word" | grep -q "$letter_sequence"
# 使用 -q 选项消去 grep 的输出结果
then
echo "$letter_sequence found in "$word"
else
echo "$letter_sequence not found in $word"
fi
if COMMAND_WHOSE_EXIT_STATUS_IS_0_UNLESS_ERROR_OCCURRED
then echo "Command succeed."
else echo "Command failed."
fi
感谢 Stéphane Chazelas 提供了后两个例子。
样例 7-1. 什么才是真?
#!/bin/bash
# 提示:
# 如果你不确定某个表达式的布尔值,可以用 if 结构进行测试。
echo
echo "Testing \"0\""
if [ 0 ]
then
echo "0 is true."
else
echo "0 is false."
fi # 0 为真。
echo
echo "Testing \"1\""
if [ 1 ]
then
echo "1 is true."
else
echo "1 is false."
fi # 1 为真。
echo
echo "Testing \"-1\""
if [ -1 ]
then
echo "-1 is true."
else
echo "-1 is false."
fi # -1 为真。
echo
echo "Testing \"NULL\""
if [ ] # NULL, 空
then
echo "NULL is true."
else
echo "NULL is false."
fi # NULL 为假。
echo
echo "Testing \"xyz\""
if [ xyz ] # 字符串
then
echo "Random string is true."
else
echo "Random string is false."
fi # 随机字符串为真。
echo
echo "Testing \"$xyz\""
if [ $xyz ] # 原意是测试 $xyz 是否为空,但是
# 现在 $xyz 只是一个没有初始化的变量。
then
echo "Uninitialized variable is true."
else
echo "Uninitialized variable is flase."
fi # 未初始化变量含有null空值,为假。
echo
echo "Testing \"-n \$xyz\""
if [ -n "$xyz" ] # 更加准确的写法。
then
echo "Uninitialized variable is true."
else
echo "Uninitialized variable is false."
fi # 未初始化变量为假。
echo
xyz= # 初始化为空。
echo "Testing \"-n \$xyz\""
if [ -n "$xyz" ]
then
echo "Null variable is true."
else
echo "Null variable is false."
fi # 空变量为假。
echo
# 什么时候 "false" 为真?
echo "Testing \"false\""
if [ "false" ] # 看起来 "false" 只是一个字符串
then
echo "\"false\" is true." #+ 测试结果为真。
else
echo "\"false\" is false."
fi # "false" 为真。
echo
echo "Testing \"\$false\"" # 未初始化的变量。
if [ "$false" ]
then
echo "\"\$false\" is true."
else
echo "\"\$false\" is false."
fi # "$false" 为假。
# 得到了我们想要的结果。
# 如果测试空变量 "$true" 会有什么样的结果?
echo
exit 0
if [ condition-true ]
then
command 1
command 2
...
else # 如果测试条件为假,则执行 else 后面的代码段
command 3
command 4
...
fi
if [ -x "$filename" ]; then
Else if 与 elif
elif
elif 是 else if 的缩写。可以把多个 if/then 语句连到外边去,更加简洁明了。
if [ condition1 ]
then
command1
command2
command3
elif [condition2 ]
# 等价于 else if
then
command4
command5
else
default-command
fi
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/bash
echo
if test -z "$1"
then
echo "No command-line arguments."
else
echo "First command-line argument is $1."
fi
echo
if /usr/bin/test -z "$1" # 等价于内建命令 "test"
# ^^^^^^^^^^^^^ # 指定全路径
then
echo "No command-line arguments."
else
echo "First command-line argument is $1."
fi
echo
if [ -z "$1" ] # 功能和上面的代码相同。
# if [ -z "$1" 理论上可行,但是 Bash 会提示缺失右括号
then
echo "No command-line arguments."
else
echo "First command-line argument is $1."
fi
echo
if /usr/bin/[ -z "$1" ] # 功能和上面的代码相同。
# if /usr/bin/[ -z "$1" # 理论上可行,但是会报错
# # 已经在 Bash 3.x 版本被修复了
then
echo "No command-line arguments."
else
echo "First command-line argument is $1."
fi
echo
exit 0