一个 二元 比较运算符比较两个变量或量。请注意,整数和字符串比较使用不同的运算符集。
等于
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" ]
这是 = 的同义词。
![]() | == 比较运算符在双中括号测试中与单中括号内表现不同。
|
不等于
if [ "$a" != "$b" ]
此运算符在 [[ ... ]] 结构中使用模式匹配。
小于,按 ASCII 字母顺序
if [[ "$a" < "$b" ]]
if [ "$a" \< "$b" ]
请注意,"<" 需要在[ ]结构中被转义。
大于,按 ASCII 字母顺序
if [[ "$a" > "$b" ]]
if [ "$a" \> "$b" ]
请注意,">" 需要在[ ]结构中被转义。
有关此比较运算符的应用,请参阅示例 27-11。
字符串为 null,即长度为零
String='' # Zero-length ("null") string variable. if [ -z "$String" ] then echo "\$String is null." else echo "\$String is NOT null." fi # $String is null. |
字符串不为 null。
示例 7-5. 算术和字符串比较
#!/bin/bash a=4 b=5 # Here "a" and "b" can be treated either as integers or strings. # There is some blurring between the arithmetic and string comparisons, #+ since Bash variables are not strongly typed. # Bash permits integer operations and comparisons on variables #+ whose value consists of all-integer characters. # Caution advised, however. echo if [ "$a" -ne "$b" ] then echo "$a is not equal to $b" echo "(arithmetic comparison)" fi echo if [ "$a" != "$b" ] then echo "$a is not equal to $b." echo "(string comparison)" # "4" != "5" # ASCII 52 != ASCII 53 fi # In this particular instance, both "-ne" and "!=" work. echo exit 0 |
示例 7-6. 测试字符串是否为 null
#!/bin/bash # str-test.sh: Testing null strings and unquoted strings, #+ but not strings and sealing wax, not to mention cabbages and kings . . . # Using if [ ... ] # If a string has not been initialized, it has no defined value. # This state is called "null" (not the same as zero!). if [ -n $string1 ] # string1 has not been declared or initialized. then echo "String \"string1\" is not null." else echo "String \"string1\" is null." fi # Wrong result. # Shows $string1 as not null, although it was not initialized. echo # Let's try it again. if [ -n "$string1" ] # This time, $string1 is quoted. then echo "String \"string1\" is not null." else echo "String \"string1\" is null." fi # Quote strings within test brackets! echo if [ $string1 ] # This time, $string1 stands naked. then echo "String \"string1\" is not null." else echo "String \"string1\" is null." fi # This works fine. # The [ ... ] test operator alone detects whether the string is null. # However it is good practice to quote it (if [ "$string1" ]). # # As Stephane Chazelas points out, # if [ $string1 ] has one argument, "]" # if [ "$string1" ] has two arguments, the empty "$string1" and "]" echo string1=initialized if [ $string1 ] # Again, $string1 stands unquoted. then echo "String \"string1\" is not null." else echo "String \"string1\" is null." fi # Again, gives correct result. # Still, it is better to quote it ("$string1"), because . . . string1="a = b" if [ $string1 ] # Again, $string1 stands unquoted. then echo "String \"string1\" is not null." else echo "String \"string1\" is null." fi # Not quoting "$string1" now gives wrong result! exit 0 # Thank you, also, Florian Wisser, for the "heads-up". |
示例 7-7. zmore
#!/bin/bash # zmore # View gzipped files with 'more' filter. E_NOARGS=85 E_NOTFOUND=86 E_NOTGZIP=87 if [ $# -eq 0 ] # same effect as: if [ -z "$1" ] # $1 can exist, but be empty: zmore "" arg2 arg3 then echo "Usage: `basename $0` filename" >&2 # Error message to stderr. exit $E_NOARGS # Returns 85 as exit status of script (error code). fi filename=$1 if [ ! -f "$filename" ] # Quoting $filename allows for possible spaces. then echo "File $filename not found!" >&2 # Error message to stderr. exit $E_NOTFOUND fi if [ ${filename##*.} != "gz" ] # Using bracket in variable substitution. then echo "File $1 is not a gzipped file!" exit $E_NOTGZIP fi zcat $1 | more # Uses the 'more' filter. # May substitute 'less' if desired. exit $? # Script returns exit status of pipe. # Actually "exit $?" is unnecessary, as the script will, in any case, #+ return the exit status of the last command executed. |
这些类似于 Bash 比较运算符 && 和 ||,用于双中括号内。
[[ condition1 && condition2 ]] |
-o 和 -a 运算符与 test 命令一起使用,或出现在单中括号测试内。
if [ "$expr1" -a "$expr2" ] then echo "Both expr1 and expr2 are true." else echo "Either expr1 or expr2 is false." fi |
![]() | 但是,正如 rihad 指出的那样
|
[1] | 正如 S.C. 指出的那样,在复合测试中,即使引用字符串变量也可能不够。[ -n "$string" -o "$a" = "$b" ]如果$string为空,则可能会在某些版本的 Bash 中导致错误。安全的方法是在可能为空的变量后附加一个额外的字符,[ "x$string" != x -o "x$a" = "x$b" ]("x's" 抵消了)。 |