ITPub博客

首页 > 数字化转型 > ERP > Comparison operators (binary)

Comparison operators (binary)

原创 ERP 作者:VampireAlchemis 时间:2007-01-13 16:43:27 0 删除 编辑

integer comparison

-eq

is equal to

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

-ne

is not equal to

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

-gt

is greater than

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

-ge

is greater than or equal to

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

-lt

is less than

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

-le

is less than or equal to

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

<

is less than (within double parentheses)

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

<=

is less than or equal to (within double parentheses)

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

>

is greater than (within double parentheses)

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

>=

is greater than or equal to (within double parentheses)

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

string comparison

=

is equal to

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

==

is equal to

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

This is a synonym for =.

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

[ $a == z* ]      # file globbing and word splitting take place
[ "$a" == "z*" ]  # true if $a is equal to z*

# Thanks, S.C.

!=

is not equal to

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

This operator uses pattern matching within a [[ ... ]] construct.

<

is less than, in ASCII alphabetical order

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

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

Note that the "<" needs to be escaped within a [ ] construct.

>

is greater than, in ASCII alphabetical order

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

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

Note that the ">" needs to be escaped within a [ ] construct.

See Example 26-4 for an application of this comparison operator.

-z

string is "null", that is, has zero length

-n

string is not "null".

Caution

The -n test absolutely requires that the string be quoted within the test brackets. Using an unquoted string with ! -z, or even just the unquoted string alone within test brackets (see Example 7-5) normally works, however, this is an unsafe practice. Always quote a tested string. [1]

Example 7-4. arithmetic and string comparisons

#!/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.

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)"
fi

# In this instance, both "-ne" and "!=" work.

echo

exit 0

Example 7-5. testing whether a string is 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


# Lets 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 ("$string1").
#
# As Stephane Chazelas points out,
#    if [ $string 1 ]   has one argument, "]"
#    if [ "$string 1" ]  has two arguments, the empty "$string1" and "]" 



echo



string1=initialized

if [ $string1 ]       # Again, $string1 stands naked.
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 naked.
then
  echo "String "string1" is not null."
else  
  echo "String "string1" is null."
fi  
# Not quoting "$string1" now gives wrong result!

exit 0
# Also, thank you, Florian Wisser, for the "heads-up".
[@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/8503278/viewspace-890171/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: File test operators
请登录后发表评论 登录
全部评论
  • 博文量
    32
  • 访问量
    240264