bash 如何在 shell 脚本中进行逻辑 OR 操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4111475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do a logical OR operation in shell scripting
提问by Strawberry
I am trying to do a simple condition check, but it doesn't seem to work.
我正在尝试做一个简单的条件检查,但它似乎不起作用。
If $#
is equal to 0
or is greater than 1
then say hello.
如果$#
等于0
或大于1
则打招呼。
I have tried the following syntax with no success:
我尝试了以下语法但没有成功:
if [ "$#" == 0 -o "$#" > 1 ] ; then
echo "hello"
fi
if [ "$#" == 0 ] || [ "$#" > 1 ] ; then
echo "hello"
fi
回答by Coding District
This should work:
这应该有效:
#!/bin/bash
if [ "$#" -eq 0 ] || [ "$#" -gt 1 ] ; then
echo "hello"
fi
I'm not sure if this is different in other shells but if you wish to use <, >, you need to put them inside double parenthesis like so:
我不确定这在其他 shell 中是否不同,但是如果您想使用 <、>,则需要将它们放在双括号中,如下所示:
if (("$#" > 1))
...
回答by jbremnant
This code works for me:
这段代码对我有用:
#!/bin/sh
argc=$#
echo $argc
if [ $argc -eq 0 -o $argc -eq 1 ]; then
echo "foo"
else
echo "bar"
fi
I don't think sh supports "==". Use "=" to compare strings and -eq to compare ints.
我不认为 sh 支持“==”。使用“=”比较字符串,使用 -eq 比较整数。
man test
for more details.
更多细节。
回答by luca76
If you are using the bash exit code status $?as variable, it's better to do this:
如果您使用的是 bash 退出代码状态$? 作为变量,最好这样做:
if [ $? -eq 4 -o $? -eq 8 ] ; then
echo "..."
fi
Because if you do:
因为如果你这样做:
if [ $? -eq 4 ] || [ $? -eq 8 ] ; then
The left part of the ORalters the $?variable, so the right part of the ORdoesn't have the original $?value.
OR的左边部分改变了$? 变量,所以OR的右侧部分没有原始的$? 价值。
回答by TechNikh
Sometimes you need to use double brackets, otherwise you get an error like too many arguments
有时需要使用双括号,否则会出现参数太多之类的错误
if [[ $OUTMERGE == *"fatal"* ]] || [[ $OUTMERGE == *"Aborting"* ]]
then
fi
回答by Peprah David
If a bash script
如果一个 bash 脚本
If [[ $input -gt number || $input -lt number ]]
then
echo .........
else
echo .........
fi
exit
回答by John Boker
have you tried something like this:
你有没有尝试过这样的事情:
if [ $# -eq 0 ] || [ $# -gt 1 ]
then
echo "$#"
fi
回答by fedorqui 'SO stop harming'
From Bash Reference Manual → 3.4.2 Special Parameters
#
($#) Expands to the number of positional parameters in decimal.
#
($#) 扩展到十进制位置参数的数量。
Therefore, $#
will always be either 0 or a bigger integer.
因此,$#
将始终为 0 或更大的整数。
So if you want to do something whenever $#
is either 0 or bigger than 1, you just have to check if $#
is or is not 1
:
因此,如果您想在$#
0 或大于 1时执行某些操作,则只需检查$#
is 或 is not 1
:
[ $# -eq 1 ] && echo "1 positional param" || echo "0 or more than 1"
This uses the syntax:
这使用语法:
[ condition ] && {things if true} || {things if false}
回答by Ravi Tyagi
And in Bash
在 Bash 中
line1=`tail -3 /opt/Scripts/wowzaDataSync.log | grep "AmazonHttpClient" | head -1`
vpid=`ps -ef| grep wowzaDataSync | grep -v grep | awk '{print }'`
echo "-------->"${line1}
if [ -z $line1 ] && [ ! -z $vpid ]
then
echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` ::
"Process Is Working Fine"
else
echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` ::
"Prcess Hanging Due To Exception With PID :"${pid}
fi
OR in Bash
或在 Bash 中
line1=`tail -3 /opt/Scripts/wowzaDataSync.log | grep "AmazonHttpClient" | head -1`
vpid=`ps -ef| grep wowzaDataSync | grep -v grep | awk '{print }'`
echo "-------->"${line1}
if [ -z $line1 ] || [ ! -z $vpid ]
then
echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` ::
"Process Is Working Fine"
else
echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` ::
"Prcess Hanging Due To Exception With PID :"${pid}
fi