本文摘自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脚本中使用逻辑或&和的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
详解shell实现ssh自动登陆
shell基本语法的介绍与使用
shell编程--grep命令如何用?
linux运维之shell变量.md
linux怎么运行shell程序
linux中shell脚本怎么运行
shell编程实战之网站文件及数据库文件的备份
linux默认使用的shell是什么
总结shell中的特殊字符
linux--bash 和 shell讲解
更多相关阅读请进入《shell》频道 >>
转载请注明出处:木庄网络博客 » 如何在shell脚本中使用逻辑或&和