博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux 中的 && 和 || 混用
阅读量:4228 次
发布时间:2019-05-26

本文共 2370 字,大约阅读时间需要 7 分钟。

一般老鸟会建议你不要在 Linux 混用这两个运算符,毕竟理解容易出问题,而且和大部分其他语言里的混用不太一样,我自己也是一知半解。不过非得要杠一下的话,也未尝不可。

我先把 《鸟哥的Linux私房菜》里的一个例子粘出来(很经典的例子):

[root@master ~]# ls exist_file && echo "exist" || echo "not exist"exist_fileexist[root@master ~]# ls no_exist_file && echo "exist" || echo "not exist"ls: cannot access no_exist_file: No such file or directorynot exist[root@master ~]# ls exist_file || echo "not exist" && echo "exist"exist_fileexist[root@master ~]# ls no_exist_file || echo "not exist" && echo "exist"ls: cannot access no_exist_file: No such file or directorynot existexist

&& 表示前一条命令执行成功时,才执行后一条命令,|| 表示上一条命令执行失败后,才执行下一条命令。 

Linux 的 && 和 || 和其他语言中的 and 和 or 还是有区别的(比如说和 Python 和 Ruby),特别是当两者混合使用的时候。

[root@master ~]# echo "hello" && echo "world" || echo "nice"# echo "hello" 执行成功,所以 && 后面的 echo "world" 也会执行,因为 # echo "world" 也会执行,所以 || 后面的 echo "nice" 就不再执行。# 可以简单理解为 || echo "nice" 被忽略了helloworld [root@master ~]# echo "hello" || echo "world" && echo "nice"# echo "hello" 执行成功,所以 || 后面的 echo "world" 不再执行,# 可以简单理解为 || echo "world" 被忽略了# 所以上式等价于 echo "hello" && echo "nice",因为 echo "hello" 执行成功,# 所以 && 后面的 echo "nice" 也会成功执行。hellonice

Python 和 Ruby 的例子好理解一些(你只要知道 print 语句本质返回 None 就差不多了, puts 返回 nil):

print("hello") or print("world") and print("nice") 等价于 (print("hello") or print("world")) and print("nice")

print("hello") 执行后返回 False, 而 print("hello") 后边是 or,所以程序还需要判断 or 后边的表达式有没有为 True 的,print("world") 执行后还是返回 False,于是 (print("hello") or print("world"))  为 False;

但是紧接着的后边是 and,而 and 前边已经是 False 了,所以 and 后边的表达式无论是 True 还是 False,整个表达式都必然是 False(所以 print("nice)" 也就没必要再去执行一遍了)。

print("hello") and print("world") or print("nice") 等价于 (print("hello") and print("world")) or print("nice")

print("hello") 执行后返回 False, 而 print("hello") 后边是 and,所以 and 后边的表达式无论是 True 还是 False,(print("hello") and print("world")) 都必然是 False(所以 print("world)" 也就没必要再去执行一遍了);

但是紧接着的后边是 or,所以程序还需要判断 or 后边的表达式有没有为 True 的,所以执行了 print("nice)" 。

Python 3.6.8 (default, Aug  7 2019, 17:28:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linuxType "help", "copyright", "credits" or "license" for more information.>>> print("hello") or print("world") and print("nice")helloworld>>> print("hello") and print("world") or print("nice")hellonice
[root@master ~]# pry[5] pry(main)> puts "hello" or puts "world" and puts "nice"helloworld=> nil[6] pry(main)> puts "hello" and puts "world" or puts "nice"hellonice=> nil

 

转载地址:http://ejjqi.baihongyu.com/

你可能感兴趣的文章
MTU 设置错误导致防火墙或者路由器断网
查看>>
子网划分详解与子网划分实例
查看>>
游戏通讯技术:帧同步技术
查看>>
防火墙技术指标---并发连接数/吞吐量
查看>>
V100服务器和T4服务器的性能指标
查看>>
elasticsearch 启动、停止及更改密码
查看>>
Kafka,它为什么速度会这么快?
查看>>
zookeeper安装启动的一些问题
查看>>
rabbitmq命令执行报错command not found
查看>>
rabbitmq基础知识介绍及总结
查看>>
StackOverFlow异常记录
查看>>
SpringMvc4.1:注解JsonView与泛型返回类
查看>>
SpringMVC+Mybatis+事务回滚+异常封装返回
查看>>
计算机网络实验报告(三):Cisco Packet Tracer 实验
查看>>
嵌入式系统基础学习笔记(九):基于 SPI 协议在 0.96 寸 OLED上【平滑显示汉字】及【温湿度数据采集显示】
查看>>
嵌入式系统基础学习笔记(十):
查看>>
网络通信编程学习笔记(七):Java与MQTT
查看>>
人工智能与机器学习学习笔记(二)
查看>>
Run Your Own Web Server Using Linux & Apache
查看>>
Java I/O
查看>>