具有多个条件的 Bash if 语句会引发错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16203088/
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
Bash if statement with multiple conditions throws an error
提问by Simply_me
I'm trying to write a script that will check two error flags, and in case one flag (or both) are changed it'll echo-- error happened. My script:
我正在尝试编写一个脚本来检查两个错误标志,如果一个标志(或两个)被更改,它将回显 - 发生错误。我的脚本:
my_error_flag=0
my_error_flag_o=0
do something.....
if [[ "$my_error_flag"=="1" || "$my_error_flag_o"=="2" ] || [ "$my_error_flag"="1" && "$my_error_flag_o"="2" ]]; then
echo "$my_error_flag"
else
echo "no flag"
fi
Basically, it should be, something along:
基本上,它应该是:
if ((a=1 or b=2) or (a=1 and b=2))
then
display error
else
no error
fi
The error I get is:
我得到的错误是:
line 26: conditional binary operator expected
line 26: syntax error near `]'
line 26: `if [[ "$my_error_flag"=="1" || "$my_error_flag_o"=="2" ] || [ "$my_error_flag"="1" && "$my_error_flag_o"="2" ]]; then'
Are my brackets messed up?
我的括号乱了吗?
回答by mkhatib
Use -a
(for and) and -o
(for or) operations.
使用-a
(for and) 和-o
(for or) 操作。
tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
Update
更新
Actually you could still use &&
and ||
with the -eq
operation. So your script would be like this:
其实你仍然可以使用&&
,并||
与-eq
操作。所以你的脚本会是这样的:
my_error_flag=1
my_error_flag_o=1
if [ $my_error_flag -eq 1 ] || [ $my_error_flag_o -eq 2 ] || ([ $my_error_flag -eq 1 ] && [ $my_error_flag_o -eq 2 ]); then
echo "$my_error_flag"
else
echo "no flag"
fi
Although in your case you can discard the last two expressions and just stick with one or operation like this:
尽管在您的情况下,您可以丢弃最后两个表达式,而只使用一个或这样的操作:
my_error_flag=1
my_error_flag_o=1
if [ $my_error_flag -eq 1 ] || [ $my_error_flag_o -eq 2 ]; then
echo "$my_error_flag"
else
echo "no flag"
fi
回答by Fizer Khan
You can use either [[
or ((
keyword. When you use [[
keyword, you have to use string operators such as -eq
, -lt
. I think, ((
is most preferred for arithmetic, because you can directly use operators such as ==
, <
and >
.
您可以使用[[
or((
关键字。使用[[
关键字时,必须使用字符串运算符,例如-eq
, -lt
。我认为,((
最适合算术,因为您可以直接使用诸如==
,<
和 之类的运算符>
。
Using [[
operator
使用[[
运算符
a=
b=
if [[ a -eq 1 || b -eq 2 ]] || [[ a -eq 3 && b -eq 4 ]]
then
echo "Error"
else
echo "No Error"
fi
Using ((
operator
使用((
运算符
a=
b=
if (( a == 1 || b == 2 )) || (( a == 3 && b == 4 ))
then
echo "Error"
else
echo "No Error"
fi
Do not use -a
or -o
operators Since it is not Portable.
不要使用-a
或-o
操作符,因为它不是便携式的。
回答by Manash Nath
Please try following
请尝试以下
if ([ $dateR -ge 234 ] && [ $dateR -lt 238 ]) || ([ $dateR -ge 834 ] && [ $dateR -lt 838 ]) || ([ $dateR -ge 1434 ] && [ $dateR -lt 1438 ]) || ([ $dateR -ge 2034 ] && [ $dateR -lt 2038 ]) ;
then
echo "WORKING"
else
echo "Out of range!"
回答by Daniel Andrei Minc?
You can get some inspiration by reading an entrypoint.sh
scriptwritten by the contributors from MySQL that checks whether the specified variables were set.
您可以通过阅读MySQL 的贡献者编写的entrypoint.sh
脚本来获得一些灵感,该脚本检查是否设置了指定的变量。
As the script shows, you can pipe them with -a
, e.g.:
如脚本所示,您可以使用管道来传输它们-a
,例如:
if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
...
fi