天天看点

条件判断

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

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

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

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

[root@localhost shell]# test -f 1 && echo true || echo false
false
[root@localhost shell]#
[root@localhost shell]#
[root@localhost shell]# test -f 1
[root@localhost shell]# test
[root@localhost shell]# test ! -f 1 && echo true || echo false
true
[root@localhost shell]#      

格式2:

[root@localhost shell]# [ -f o ] && echo 0 || echo 1
1
[root@localhost shell]# touch o
[root@localhost shell]# [ -f o ] && echo 0 || echo 1
0
[root@localhost shell]#      

和test 用法基本一样。

格式3:

[root@localhost shell]# [[ -f o && -d tt ]] && echo 0 || echo 1
1
[root@localhost shell]# mkdir tt
[root@localhost shell]# [[ -f o && -d tt ]] && echo 0 || echo 1
0
[root@localhost shell]#      

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

条件判断

记忆方法:

#help test

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

上一篇: 条件判断
下一篇: 条件判断