Bash Shell脚本if语句压缩字符串比较和布尔检查

Bash Shell脚本if语句压缩字符串比较和布尔检查,bash,shell,ubuntu,if-statement,sh,Bash,Shell,Ubuntu,If Statement,Sh,我有一个函数,其中我试图将第一个参数与字符串进行比较,然后是一个单独的布尔变量 我的示例布尔: EMS=true; COLL=true; function some_function() { ... ... if [ [ "$1" = "ez" ] && $EMS ] || [ "$1" = "coll" ] && $COLL ]; then #do mysterious magic ... ... fi } so

我有一个函数,其中我试图将第一个参数与字符串进行比较,然后是一个单独的布尔变量

我的示例布尔:

EMS=true; 
COLL=true;
function some_function() {
...
...
     if [ [ "$1" = "ez" ] &&  $EMS ] || [ "$1" = "coll" ] &&  $COLL ]; then
     #do mysterious magic
     ...
     ...
fi
}
some_function ez
some_function coll
if ("$1" is "ez" AND $EMS evaluates to true) OR ("$1" is "coll" AND $COL evaluates to true)
在给定的点上,两者或其中任何一个都可能为真

我的函数体:

EMS=true; 
COLL=true;
function some_function() {
...
...
     if [ [ "$1" = "ez" ] &&  $EMS ] || [ "$1" = "coll" ] &&  $COLL ]; then
     #do mysterious magic
     ...
     ...
fi
}
some_function ez
some_function coll
if ("$1" is "ez" AND $EMS evaluates to true) OR ("$1" is "coll" AND $COL evaluates to true)
我这样调用函数:

EMS=true; 
COLL=true;
function some_function() {
...
...
     if [ [ "$1" = "ez" ] &&  $EMS ] || [ "$1" = "coll" ] &&  $COLL ]; then
     #do mysterious magic
     ...
     ...
fi
}
some_function ez
some_function coll
if ("$1" is "ez" AND $EMS evaluates to true) OR ("$1" is "coll" AND $COL evaluates to true)
但是,当我执行脚本时,我遇到以下情况:

./deployBuild.sh: line 145: [: too many arguments
我的if循环不正确,无法修复。我如何继续

我想要实现的伪代码:

EMS=true; 
COLL=true;
function some_function() {
...
...
     if [ [ "$1" = "ez" ] &&  $EMS ] || [ "$1" = "coll" ] &&  $COLL ]; then
     #do mysterious magic
     ...
     ...
fi
}
some_function ez
some_function coll
if ("$1" is "ez" AND $EMS evaluates to true) OR ("$1" is "coll" AND $COL evaluates to true)

在最近的Bash中,您可以尝试:

if  [[  ( "$1" = "ez"   &&  $EMS ) || (  "$1" = "coll"  &&  $COLL ) ]]
更便携的解决方案是:

if  [  \( "$1" = "ez"   -a  $EMS \) -o \(  "$1" = "coll"  -a  $COLL \) ]

在最近的Bash中,您可以尝试:

if  [[  ( "$1" = "ez"   &&  $EMS ) || (  "$1" = "coll"  &&  $COLL ) ]]
更便携的解决方案是:

if  [  \( "$1" = "ez"   -a  $EMS \) -o \(  "$1" = "coll"  -a  $COLL \) ]

@xaiwi给了你正确的答案:用
[[
代替
[
——检查 在bash手册中

但你的逻辑是错误的:

if [[ ( "$1" = "ez" &&  $EMS ) || ( "$1" = "coll"  &&  $COLL) ]]; 
如果值
$EMS
位于
[…]]
内,则如果值非空,则会得到“true”结果-请参阅手册中的
-n

因为“true”是bash命令,所以您可能需要

if ([[ $1 = "ez" ]] &&  $EMS) || ([[ $1 = "coll" ]] && $COLL); then ...
if { [[ $1 = "ez" ]] &&  $EMS; } || { [[ $1 = "coll" ]] && $COLL; }; then ... 

第一个使用子shell进行命令分组;第二个使用当前的shell分组语法--。

@xaiwi给了您正确的答案:使用
[
而不是
[
--check 在bash手册中

但你的逻辑是错误的:

if [[ ( "$1" = "ez" &&  $EMS ) || ( "$1" = "coll"  &&  $COLL) ]]; 
如果值
$EMS
位于
[…]]
内,则如果值非空,则会得到“true”结果-请参阅手册中的
-n

因为“true”是bash命令,所以您可能需要

if ([[ $1 = "ez" ]] &&  $EMS) || ([[ $1 = "coll" ]] && $COLL); then ...
if { [[ $1 = "ez" ]] &&  $EMS; } || { [[ $1 = "coll" ]] && $COLL; }; then ... 

第一个使用子shell进行命令分组;第二个使用当前的shell分组语法--。

此外,您不能包装
[
--我添加了另一个副本(间接)解释了这一点。此外,您不能包装
[
--我添加了另一个副本(间接)解释了这一点。