linux中命令间隔符; 、||和 && 的区别

 

001、

root@PC1:/home/test2# pwd                 ## 一个正常的linux命令
/home/test2
root@PC1:/home/test2# pwd; echo "xxx"     ##;前执行正常,后面的命令能够正常执行
/home/test2
xxx
root@PC1:/home/test2# pwd && echo "xxx"   ## &&前执行正常,后面的命令也能正常执行
/home/test2
xxx

 

002、

root@PC1:/home/test2# 1234                            ## 输入一个非linux命令
1234: command not found
root@PC1:/home/test2# 1234; echo "hello world"        ## 使用;间隔符,前一个命令执行失败,仍然执行后面的命令
1234: command not found
hello world
root@PC1:/home/test2# 1234 && echo "hello world"      ## 使用&&间隔符,前一个命令失败, 则不执行后面的命令
1234: command not found

 

003、

root@PC1:/home/test2# pwd
/home/test2
root@PC1:/home/test2# pwd || echo "xxx"      ## || 前面的命令正常执行,则 || 后面的命令不再执行
/home/test2
root@PC1:/home/test2# pwd && echo "xxx"      ## && 前面的命令正常执行, 则 && 后面的命令能够执行
/home/test2
xxx

 

004、

root@PC1:/home/test2# 1234
1234: command not found
root@PC1:/home/test2# 1234 || echo "xxx"     ## || 前面的命令不能正常执行,则执行||后面的命令
1234: command not found
xxx
root@PC1:/home/test2# 1234 && echo "xxx"     ## && 前面的命令不能正常执行,则不执行&&后面的命令
1234: command not found

 

||:在前面命令执行错误时,才执行后面的命令。

&&: 只有前面命令正确执行时, 才执行后面的命令。

;:无论前面的命令是否正确执行,将执行整个脚本命令。

 

posted @ 2022-05-04 08:26  小鲨鱼2018  阅读(2083)  评论(0编辑  收藏  举报