登录站点

用户名

密码

7.3. Other Comparison Operators

已有 191 次阅读  2006-09-10 22:03   标签Linux  Advanced  Bash-Scripting  Guide  bash 
1、太复杂了,记不住的,有没有什么规则呀?
The == comparison operator behaves differently within a double-brackets         test than within single brackets.

            1 [[ $a == z* ]]    # True if $a starts with an "z" (pattern matchin            2 [[ $a == "z*" ]]  # True if $a is equal to z* (literal matching).
            3
            4 [ $a == z* ]      # File globbing and word splitting take place.
            5 [ "$a" == "z*" ]  # True if $a is equal to z* (literal matching).
            6
            7 # Thanks, St?phane Chazelas

2、测试数值用[ "$a" -ne "$b" ],测试字符串用[ "$a" != "$b" ]。

3、Example 7-6. Testing whether a string is null通过几个例子反复说:测试字符串一定要用双引号把字符串变量括起来!从前面看,测试数值变量同样是把变量用双引号括起来的,这样,可以总结说,无论是数值变量和字符串变量都必须加双引号。

4、Example 7-7. zmore中测试文件类型的语句,不懂

if [ ${filename##*.} != "gz" ]
# Using bracket in variable substitution.
then
  echo "File $1 is not a gzipped file!"
  exit $NOTGZIP
fi


5、复合比较:
1)、[[ condition1 && condition2 ]]相当于 [ "$exp1" -a "$exp2" ]
2)、在比较空字符串时,应该在有可能是空字符串的字符串前加上额外的字符,文中说:

As S.C. points out, in a compound test, even quoting the string variable
     might not suffice. [ -n "$string" -o "$a" = "$b" ] may cause an error with
     some versions of Bash if $string is empty. The safe way is to append an
     extra character to possibly empty variables, [ "x$string" != x -o "x$a" =
     "x$b" ] (the "x's" cancel out).




英语疑问:
1、globbing
File globbing and word splitting take place.

2、Caution advised, however.
  10 #  Bash permits integer operations and comparisons on variables
  11 #+ whose value consists of all-integer characters.
  12 #  Caution advised, however.

3、sealing wax, cabbages and kings
   2 #  str-test.sh: Testing null strings and unquoted strings,
   3 #+ but not strings and sealing wax, not to mention cabbages and kings . . .

4、compound comparison

5、suffice:
As S.C. points out, in a compound test, even quoting the string variable
     might not suffice.

6、the "x's" cancel out(上面5的2))
分享 举报