本文摘自PHP中文网,作者不言,侵删。
在我们的程序(脚本)中使用多个条件时,逻辑或&与操作非常有用。或在两个或多个条件之间使用。如果任何一个条件返回为true,则返回真。与在两个或多个条件之间使用。只有当所有条件都返回为真时,才会返回true。
使用逻辑或
逻辑或在bash脚本中与运算符-o一起使用。下面的shell脚本将展示如何在两个条件之间使用逻辑或(-o)。
1 2 3 4 5 6 7 8 9 | #!/bin/bash
read -p "Enter First Numeric Value: " first
read -p "Enter Second Numeric Value: " second
if [ $ first -le 10-o$ second -gt 20 ]
then
echo "OK"
else
echo "Not OK"
fi
|
使用逻辑与
bash脚本中的逻辑与和运算符-a一起使用。下面的shell脚本将展示如何在两个条件之间使用逻辑与(-a)。
1 2 3 4 5 6 7 8 9 10 11 12 | #!/bin/bash
read -p "Enter First Numeric Value: " first
read -p "Enter Second Numeric Value: " second
if [ $ first -le 10-a$ second -gt 20 ]
then
echo "OK"
else
echo "Not OK"
fi
|
逻辑或&与混合使用
下面的示例将介绍如何在单个语句中使用多个逻辑运算符。
1 2 3 4 5 6 7 8 | #!/bin/bash
read -p "Enter Your Number:" i
if [ ( $i -ge 10 -a $i -le 20 ) -o ( $i -ge 100 -a $i -le 200 ) ]
then
echo "OK"
else
echo "Not OK"
fi
|
本篇文章到这里就已经全部结束了,更多其他精彩内容可以关注PHP中文网的Linux教程视频栏目!
以上就是如何在shell脚本中使用逻辑或&和的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
linux怎么执行shell脚本
shell脚本高级编程的详细讲解
linux怎么执行shell脚本
linux怎么运行shell程序
shell编程实战之监控网卡流量
bash shell:测试文件或目录是否存在
如何在shell脚本中为用户分配密码
shell脚本实现的乘法表实例
如何调试shell脚本?
shell判断字符串为空的方法
更多相关阅读请进入《shell》频道 >>
转载请注明出处:木庄网络博客 » 如何在shell脚本中使用逻辑或&和