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 @   小鲨鱼2018  阅读(2158)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-05-04 c语言中函数的定义和调用
2020-05-04 《Linux就该这么学》学习笔记 03
点击右上角即可分享
微信分享提示