linux系統中邏輯測試語句
阿新 • • 發佈:2020-10-14
1、&& 表示與,上一句執行成功則執行下一句;|| 表示或,上一句執行失敗則執行下一句
[root@linuxprobe test]# touch a.txt
[root@linuxprobe test]# ls
a.txt
[root@linuxprobe test]# [ -e a.txt ] && mkdir test ## a.txr存在,&&連線則會執行下一句
[root@linuxprobe test]# ls
a.txt test
[root@linuxprobe test]# [ -e b.txt ] && mkdir test2 ## b.txt不存在,&&連線不執行下一句
[root@linuxprobe test]# ls
a.txt test
[root@linuxprobe test]# ls
a.txt test
[root@linuxprobe test]# [ -e a.txt ] || mkdir test3 ## a.txt存在,不執行|| 後面的語句
[root@linuxprobe test]# ls
a.txt test
[root@linuxprobe test]# [ -e b.txt ] || mkdir test4 ## b.txt不存在,執行||後面的語句
[root@linuxprobe test]# ls
a.txt test test4
2、結合使用
[root@linuxprobe test]# [ -e a.txt ] && mkdir test5 || mkdir test6
[root@linuxprobe test]# ls
a.txt test test4 test5
[root@linuxprobe test]# [ -e b.txt ] && mkdir test6 || mkdir test7
[root@linuxprobe test]# ls
a.txt test test4 test5 test7
[root@linuxprobe test]# [ -e a.txt ] || mkdir test8 && mkdir test9 ## &&和||順序無影響
[root@linuxprobe test]# ls
a.txt test test4 test5 test7 test9
[root@linuxprobe test]# [ -e b.txt ] || mkdir test10 && mkdir test11
[root@linuxprobe test]# ls
a.txt test test10 test11 test4 test5 test7 test9