linux 中 && 用法

 

001、

[root@pc1 test2]# ls
test.sh
[root@pc1 test2]# cat test.sh      ## 测试程序
#!/bin/bash

seq 10 > a.txt

xxxxxx  && echo 20 done ; rm a.txt
[root@pc1 test2]# bash test.sh       ## a.txt被删除了,代码rm a.txt属于单独的代码块
test.sh: line 5: xxxxxx: command not found
[root@pc1 test2]# ls
test.sh

 

002、修改程序

[root@pc1 test2]# ls
test.sh
[root@pc1 test2]# cat test.sh         ## 测试程序
#!/bin/bash

seq 10 > a.txt

xxxxxx  && echo 20 done && rm a.txt
[root@pc1 test2]# bash test.sh    
test.sh: line 5: xxxxxx: command not found
[root@pc1 test2]# ls                  ## a.txt被保留,&&的作用是在上一个代码块执行成功的情况下,才执行下一个代码块
a.txt  test.sh

 

posted @ 2023-09-21 11:06  小鲨鱼2018  阅读(82)  评论(0编辑  收藏  举报