|NO.Z.00039|——————————|LinuxShell|——|Linux&Shell$Bash功能.V05|——|多命令执行顺序|

一、多命令顺序执行
  格式 作用
; 命令1;命令2 多个命令顺序执行,命令之间没有任何逻辑联系
&& 命令1&&命令2 当命令1正确执行(?=0),21? 0)则命令2不会执行
|| 命令1||命令2 当命令1执行不正确(?0)21?=0),则命令2不会执行

二、多命令顺序执行实验

### --- 分号执行
~~~     # 分号:
~~~     多个命令顺序执行;命令之间加入分号;命令之间不需要有任何联系

[root@localhost ~]# ls ; date ; cd /etc 
abc  bac  err  hello.sh  ok
Sat May 15 23:03:24 CST 2021
### --- 假设ls报错,但是不影响下一条命令执行

[root@localhost ~]# lslll ; date ; cd /etc 
-bash: lslll: command not found
Sat May 15 23:04:17 CST 2021 
[root@localhost etc]# 
### --- 逻辑与&&:
~~~     当命令1正确执行,命令2才会执行;
~~~     若是命令1不正确执行,命令2是不执行的;命令之间是没有任何关系的
~~~     注:命令1正确执行,命令2执行输出yes

[root@localhost ~]# ls && echo yes
err  hello.sh  ok
yes
~~~     注:命令1不正确执行,命令2不执行

[root@localhost ~]# lsll && echo yes
-bash: lsll: command not found
### --- 逻辑或||:命令1不正确执行,命令2才会执行,若是命令1正确执行,命令2不执行
~~~     注:命令1正确执行,命令2不执行
~~~     注:命令1不正确执行,命令2执行输出no

[root@localhost ~]# ls || echo no
err  hello.sh  ok
[root@localhost ~]# lsll || echo no
-bash: lsll: command not found
no
### --- 判断命令是否正确执行
~~~     注:命令1正确执行,输出yes
~~~     命令1错误执行,输出no

[root@localhost ~]# ls && echo yes || echo no
err  hello.sh  ok
yes
[root@localhost ~]# lsll && echo yes || echo no
-bash: lsll: command not found
no
### --- ||和&&写法
### --- 先写|| 后写&&;是不可以的
~~~     注:输出是没有问题的,但是echo no没有执行

[root@localhost ~]# ls || echo no && echo yes
err  hello.sh  ok
yes
~~~     no和yes全部执行,不和常规思维模式

[root@localhost ~]# lswwww || echo no && echo yes
-bash: lswwww: command not found
no
yes         

 

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

posted on   yanqi_vip  阅读(16)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示