格式1:#test<测试表达式>

格式2:#[<测试表达式>]

格式3:#[[<测试表达式>]]


说明:格式1 和 格式2是等价的。



[[email protected] shell]# test -f 1 && echo true || echo false
false
[[email protected] shell]#
[[email protected] shell]#
[[email protected] shell]# test -f 1
[[email protected] shell]# test
[[email protected] shell]# test ! -f 1 && echo true || echo false
true
[[email protected] shell]#

格式2:

[[email protected] shell]# [ -f o ] && echo 0 || echo 1
1
[[email protected] shell]# touch o
[[email protected] shell]# [ -f o ] && echo 0 || echo 1
0
[[email protected] shell]#

和test 用法基本一样。


格式3:

[[email protected] shell]# [[ -f o && -d tt ]] && echo 0 || echo 1
1
[[email protected] shell]# mkdir tt
[[email protected] shell]# [[ -f o && -d tt ]] && echo 0 || echo 1
0
[[email protected] shell]#

要在里面写多种条件时用两个` ` 这样才行。









条件判断



记忆方法:

#help test



###############################


条件判断