7.3. 其他比较运算符

一个 二元 比较运算符比较两个变量或量。请注意,整数和字符串比较使用不同的运算符集。

整数比较

-eq

等于

if [ "$a" -eq "$b" ]

-ne

不等于

if [ "$a" -ne "$b" ]

-gt

大于

if [ "$a" -gt "$b" ]

-ge

大于或等于

if [ "$a" -ge "$b" ]

-lt

小于

if [ "$a" -lt "$b" ]

-le

小于或等于

if [ "$a" -le "$b" ]

<

小于 (在双括号内)

(("$a" < "$b"))

<=

小于或等于 (在双括号内)

(("$a" <= "$b"))

>

大于 (在双括号内)

(("$a" > "$b"))

>=

大于或等于 (在双括号内)

(("$a" >= "$b"))

字符串比较

=

等于

if [ "$a" = "$b" ]

Caution

注意框架 =空格

if [ "$a"="$b" ]与上述等效。

==

等于

if [ "$a" == "$b" ]

这是 = 的同义词。

Note

== 比较运算符在双中括号测试中与单中括号内表现不同。

[[ $a == z* ]]   # True if $a starts with an "z" (pattern matching).
[[ $a == "z*" ]] # True if $a is equal to z* (literal matching).

[ $a == z* ]     # File globbing and word splitting take place.
[ "$a" == "z*" ] # True if $a is equal to z* (literal matching).

# Thanks, St�phane Chazelas

!=

不等于

if [ "$a" != "$b" ]

此运算符在 [[ ... ]] 结构中使用模式匹配。

<

小于,按 ASCII 字母顺序

if [[ "$a" < "$b" ]]

if [ "$a" \< "$b" ]

请注意,"<" 需要在[ ]结构中被转义

>

大于,按 ASCII 字母顺序

if [[ "$a" > "$b" ]]

if [ "$a" \> "$b" ]

请注意,">" 需要在[ ]结构中被转义

有关此比较运算符的应用,请参阅示例 27-11

-z

字符串为 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.

-n

字符串不为 null。

Caution

The-n测试要求字符串在测试括号内被引用。将未加引号的字符串与 ! -z 一起使用,甚至仅在测试括号内使用未加引号的字符串(请参阅示例 7-6)通常可以工作,但是,这是一种不安全的做法。始终引用被测试的字符串。[1]

示例 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.

复合比较

-a

逻辑与

exp1 -a exp2如果 exp1exp2 都为真,则返回真。

-o

逻辑或

exp1 -o exp2如果 exp1 exp2 为真,则返回真。

这些类似于 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

Caution

但是,正如 rihad 指出的那样

[ 1 -eq 1 ] && [ -n "`echo true 1>&2`" ]   # true
[ 1 -eq 2 ] && [ -n "`echo true 1>&2`" ]   # (no output)
# ^^^^^^^ False condition. So far, everything as expected.

# However ...
[ 1 -eq 2 -a -n "`echo true 1>&2`" ]       # true
# ^^^^^^^ False condition. So, why "true" output?

# Is it because both condition clauses within brackets evaluate?
[[ 1 -eq 2 && -n "`echo true 1>&2`" ]]     # (no output)
# No, that's not it.

# Apparently && and || "short-circuit" while -a and -o do not.

请参阅示例 8-3示例 27-17示例 A-29,以查看复合比较运算符的实际应用。

注释

[1]

正如 S.C. 指出的那样,在复合测试中,即使引用字符串变量也可能不够。[ -n "$string" -o "$a" = "$b" ]如果$string为空,则可能会在某些版本的 Bash 中导致错误。安全的方法是在可能为空的变量后附加一个额外的字符,[ "x$string" != x -o "x$a" = "x$b" ]"x's" 抵消了)。