本文摘自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--bash 和 shell讲解
启动bash shell有哪些方式?
shell如何实现学生成绩管理系统
shell编程实战之监控网卡流量
linux中bash是什么意思?
shell编程实战之执行多个目录下的php程序
linux默认使用的shell是什么
如何调试shell脚本?
启动bash shell有哪些方式?
shell脚本实现的乘法表实例
更多相关阅读请进入《shell》频道 >>
转载请注明出处:木庄网络博客 » 如何在shell脚本中使用逻辑或&和