| Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting | ||
|---|---|---|
| Prev | Chapter 7. Tests | Next |
二元比较操作符比较两个变量或是数值。注意整数和字符串比较的分别。
等于
if [ "$a" -eq "$b" ]
不等于
if [ "$a" -ne "$b" ]
大于
if [ "$a" -gt "$b" ]
大于等于
if [ "$a" -ge "$b" ]
小于
if [ "$a" -lt "$b" ]
小于等于
if [ "$a" -le "$b" ]
小于(在双括号里使用)
(("$a" < "$b"))
小于等于 (在双括号里使用)
(("$a" <= "$b"))
大于 (在双括号里使用)
(("$a" > "$b"))
大于等于(在双括号里使用)
(("$a" >= "$b"))
等于
if [ "$a" = "$b" ]
等于
if [ "$a" == "$b" ]
它和=是同义词。
![]() |
==比较操作符在一个双方括号测试和一个单方括号号里意思不同。
|
不相等
if [ "$a" != "$b" ]
操作符在[[ ... ]]结构里使用模式匹配.
小于,依照ASCII字符排列顺序
if [[ "$a" < "$b" ]]
if [ "$a" \< "$b" ]
注意"<"字符在[ ] 结构里需要转义
大于,依照ASCII字符排列顺序
if [[ "$a" > "$b" ]]
if [ "$a" \> "$b" ]
注意">"字符在[ ] 结构里需要转义.
参考例子 26-11 中这种比较的一个应用.
字符串为"null",即是指字符串长度为零。
字符串不为"null",即长度不为零.
例子 7-5. 算术和字符串比较
1 #!/bin/bash 2 3 a=4 4 b=5 5 6 # 这儿的"a"和"b"既能被看成是整数也能被看着是字符串。 7 # 在字符串和数字之间不是严格界定的, 8 #+ 因为Bash变量不是强类型的。 9 10 # Bash允许变量的整数操作和比较, 11 #+ 但这些变量必须只包括数字字符. 12 # 不管怎么样,你应该小心考虑. 13 14 echo 15 16 if [ "$a" -ne "$b" ] 17 then 18 echo "$a is not equal to $b" 19 echo "(arithmetic comparison)" 20 fi 21 22 echo 23 24 if [ "$a" != "$b" ] 25 then 26 echo "$a is not equal to $b." 27 echo "(string comparison)" 28 # "4" != "5" 29 # ASCII 52 != ASCII 53 30 fi 31 32 # 在这个实际的例子中,"-ne"和"!="都能工作. 33 34 echo 35 36 exit 0 |
例子 7-6. 测试一个字符串是否是null值
1 #!/bin/bash
2 # str-test.sh: 测试null字符串和没有用引号引起的字符串,
3 #+ but not strings and sealing wax, not to mention cabbages and kings . . .
4
5 # 用if [ ... ]结构
6
7
8 # 如果一个字符串变量没有被初始化,它里面的值未定义。
9 # 这种情况称为"null"值(不同于零).
10
11 if [ -n $string1 ] # $string1没有被声明或初始化.
12 then
13 echo "String \"string1\" is not null."
14 else
15 echo "String \"string1\" is null."
16 fi
17 # 错误结果.
18 # Shows $string1 as not null, although it was not initialized.
19
20
21 echo
22
23
24 # 让我们再试试.
25
26 if [ -n "$string1" ] # 这次, $string1变量被引号引起.
27 then
28 echo "String \"string1\" is not null."
29 else
30 echo "String \"string1\" is null."
31 fi # 在一个测试方括里用引号引起变量!
32
33
34 echo
35
36
37 if [ $string1 ] # 这次,变量$string1什么也不加.
38 then
39 echo "String \"string1\" is not null."
40 else
41 echo "String \"string1\" is null."
42 fi
43 # This works fine.
44 # The [ ] test operator alone detects whether the string is null.
45 # However it is good practice to quote it ("$string1").
46 #
47 # As Stephane Chazelas points out,
48 # if [ $string1 ] has one argument, "]"
49 # if [ "$string1" ] has two arguments, the empty "$string1" and "]"
50
51
52
53 echo
54
55
56
57 string1=initialized
58
59 if [ $string1 ] # Again, $string1 stands naked.
60 then
61 echo "String \"string1\" is not null."
62 else
63 echo "String \"string1\" is null."
64 fi
65 # Again, gives correct result.
66 # Still, it is better to quote it ("$string1"), because . . .
67
68
69 string1="a = b"
70
71 if [ $string1 ] # Again, $string1 stands naked.
72 then
73 echo "String \"string1\" is not null."
74 else
75 echo "String \"string1\" is null."
76 fi
77 # Not quoting "$string1" now gives wrong result!
78
79 exit 0
80 # Thank you, also, Florian Wisser, for the "heads-up". |
例子 7-7. zmore
1 #!/bin/bash
2 # zmore
3
4 #用'more'查看被压缩过的文件
5
6 NOARGS=65
7 NOTFOUND=66
8 NOTGZIP=67
9
10 if [ $# -eq 0 ] # 等同于: if [ -z "$1" ]
11 # $1可以存在,但为空: zmore "" arg2 arg3
12 then
13 echo "Usage: `basename $0` filename" >&2
14 # 错误信息打印到标准错误.
15 exit $NOARGS
16 # 返回65作为脚本的退出状态(错误码).
17 fi
18
19 filename=$1
20
21 if [ ! -f "$filename" ] # Quoting $filename allows for possible spaces.
22 then
23 echo "File $filename not found!" >&2
24 # 错误信息打印到标准错误.
25 exit $NOTFOUND
26 fi
27
28 if [ ${filename##*.} != "gz" ]
29 # 在方括号里使用变量替换.
30 then
31 echo "File $1 is not a gzipped file!"
32 exit $NOTGZIP
33 fi
34
35 zcat $1 | more
36
37 # 用过滤程序'more.'
38 # 如果你想,也可以用'less'代替。
39
40
41 exit $? # 脚本返回管道的退出状态
42 # 实际上"exit $?"是多余的,因为在任何情况下,
43 # 脚本会返回最后一个执行命令的退出码。 |
它们和Bash中用于双方括号结构的比较操作符&&和||很相似。
1 [[ condition1 && condition2 ]] |
1 if [ "$exp1" -a "$exp2" ] |
可以参考Example 8-3, Example 26-16, 和Example A-28怎么实际地使用混合比较操作符。
| [1] | 就像S.C.指出的那样,在一个混合测试中,把一个字符串变量引号引起来可能还不够。如果$string变量是空的话,表达式[ -n "$string" -o "$a" = "$b" ]在一些Bash版本中可能会引起错误。安全的办法是附加一个外部的字符串给可能有空字符串变量比较的所有变量,[ "x$string" != x -o "x$a" = "x$b" ](x字符可以互相抵消)。 |