天天看點

條件判斷

格式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

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

上一篇: 條件判斷
下一篇: 條件判斷