linux-命令1&&命令2||命令3

一. 必须&&在前||在后

二. 命令1可以是以下几种

2.1 普通命令

ls && echo yes || echo no
echo 123 |grep 123 && echo yes || echo no

2.2 变量=$(命令)

localhost:~ # a=`ls` && echo yes || echo no
yes

localhost:~ # a=$(ls) && echo yes || echo no
yes

2.3 test 条件

localhost:~ # test "abc" == "abc" && echo yes || echo no
yes

2.4 [ 条件 ]

localhost:~ # [ "abc" == "abc" ] && echo yes || echo no
yes

三. 命令2和命令3是多条语句-用()在子shell执行

[root@ansible9 ~]# 222&&(echo yes;cd test;touch yes.txt)||(echo no;cd test;touch no.txt)
bash: 222: command not found...
no
[root@ansible9 ~]# ll ./no.txt
ls: cannot access './no.txt': No such file or directory
[root@ansible9 ~]# ll test/no.txt 
-rw-r--r-- 1 root root 0 Dec 30 11:49 test/no.txt
[root@ansible9 ~]# 

四. 命令2和命令3是多条语句-用{}在本shell执行

[root@ansible9 ~]# 222&&{ echo yes;cd test;touch yes.txt; }||{ echo no;cd test;touch no.txt; }
bash: 222: command not found...
no
[root@ansible9 test]# ll no.txt
-rw-r--r-- 1 root root 0 Dec 30 11:53 no.txt
[root@ansible9 test]# 

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值