linux系统中逻辑测试语句

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
原文地址:https://www.cnblogs.com/liujiaxin2018/p/13814070.html