shell 测试条件命令
http://blog.csdn.net/yangruibao/article/details/7427503
test 和 [ 命令
虽然 Linux 和 UNIX 的每个版本中都包含 test 命令,但该命令有一个更常用的别名 — 左方括号:[。test 及其别名通常都可以在 /usr/bin 或 /bin (取决于操作系统版本和供应商)中找到。
当您使用左方括号而非 test 时,其后必须始终跟着一个空格、要评估的条件、一个空格和右方括号。右方括号不是任何东西的别名,而是表示所需评估参数的结束。条件两边的空格是必需的,这表示要调用 test,以区别于同样经常使用方括号的字符/模式匹配操作。
test 和 [ 的语法如下:
test expression
[ expression ]
在这两种情况下,test 都评估一个表达式,然后返回真或假。如果它和 if、while 或 until 命令结合使用,则您可以对程序流进行广泛的控制。不过,您无需将 test 命令与任何其它结构一起使用;您可以从命令行直接运行它来检查几乎任何东西的状态。
参考总结
http://blog.csdn.net/vah101/article/details/6249006
三种方式
https://www.ibm.com/developerworks/library/l-bash-test/index.html
test and [
The
test
builtin command returns 0 (True) or 1 (False), depending on the evaluation of an expression, expr. You can also use square brackets:test expr
and [ expr ] are equivalent. You can examine the return value by displaying$?
; you can use the return value with && and ||; or you can test it using the various conditional constructs that are covered later in this tip.Listing 1. Some simple tests
123456[ian@pinguino ~]$ test 3 -gt 4 && echo True || echo false
false
[ian@pinguino ~]$ [ "abc" != "def" ];echo $?
0
[ian@pinguino ~]$ test -d "$HOME" ;echo $?
0
(( and [[
The
test
command is very powerful, but somewhat unwieldy given its requirement for escaping and given the difference between string and arithmetic comparisons. Fortunately, bash has two other ways of testing that are somewhat more natural for people who are familiar with C, C++, or Java® syntax.The
(( ))
compound command evaluates an arithmetic expression and sets the exit status to 1 if the expression evaluates to 0, or to 0 if the expression evaluates to a non-zero value. You do not need to escape operators between((
and))
. Arithmetic is done on integers. Division by 0 causes an error, but overflow does not. You may perform the usual C language arithmetic, logical, and bitwise operations. Thelet
command can also execute one or more arithmetic expressions. It is usually used to assign values to arithmetic variables.Listing 5. Assigning and testing arithmetic expressions
123456[ian@pinguino ~]$ let x=2 y=2**3 z=y*3;echo $? $x $y $z
0 2 8 24
[ian@pinguino ~]$ (( w=(y/x) + ( (~ ++x) & 0x0f ) )); echo $? $x $y $w
0 3 8 16
[ian@pinguino ~]$ (( w=(y/x) + ( (~ ++x) & 0x0f ) )); echo $? $x $y $w
0 4 8 13
As with
(( ))
, the[[ ]]
compound command allows you to use more natural syntax for filename and string tests. You can combine tests that are allowed for thetest
command using parentheses and logical operators.Listing 6. Using the [[ compound
123[ian@pinguino ~]$ [[ ( -d "$HOME" ) && ( -w "$HOME" ) ]] &&
> echo "home is a writable directory"
home is a writable directory
The
[[
compound can also do pattern matching on strings when the=
or!=
operators are used. The match behaves as for wildcard globbing as illustrated in Listing 7.Listing 7. Wildcard tests with [[
123456[ian@pinguino ~]$ [[ "abc def .d,x--" == a[abc]* ?d* ]]; echo $?
0
[ian@pinguino ~]$ [[ "abc def c" == a[abc]* ?d* ]]; echo $?
1
[ian@pinguino ~]$ [[ "abc def d,x" == a[abc]* ?d* ]]; echo $?
1
You can even do arithmetic tests within
[[
compounds, but be careful. Unless within a((
compound, the<
and>
operators will compare the operands as strings and test their order in the current collating sequence. Listing 8 illustrates this with some examples.
测试种类参考:
https://linuxacademy.com/blog/linux/conditions-in-bash-scripting-if-statements/
test 和 [[ 本质不同
https://stackoverflow.com/questions/14496428/meaning-of-double-square-brackets-in-bash
[[ is a much more versatile version of [ in bash.
For example, Using the [[ ... ]] test construct, rather than [ ... ] can prevent many logic errors in scripts. For example, the &&, ||, <, and > operators work within a [[ ]] test, despite giving an error within a [ ] construct.
also, their implementations are different
-bash-3.2$ type [
[ is a shell builtin
-bash-3.2$ type [[
[[ is a shell keyword
Refer here for a good explanation