Bash 不允许在 case 语句中使用 if 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17949857/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:04:00  来源:igfitidea点击:

Bash does not allow if statement inside case statement

bash

提问by user2367078

Can someone help me find out what I am doing wrong on this bash script. I am trying to using if statement inside case statement and bash is complaining syntax error.

有人可以帮我找出我在这个 bash 脚本上做错了什么。我正在尝试在 case 语句中使用 if 语句,而 bash 抱怨语法错误。

findinfo() {
OPT1=
case  "$OPT1" in
   linux)
        echo "Setting environment"
        ESC="hello_linux" if [[ "$PROJN" == "ONE" ||  "$PROJN" == "two" ]]
        ;;
   Windows)
        echo "Setting environment"
        ESC="hello_windows" if [[ "$PROJN" == "ONE" ||  "$PROJN" == "two" ]]
        ;;
   Android)
        echo "Setting environment"
        ESC="hello_android" if [[ "$PROJN" == "ONE" ||  "$PROJN" == "two" ]]
        ;;
esac

}

Thanks

谢谢

回答by choroba

In bash, ifmust precede the thenpart:

在 bash 中,if必须在then部分之前:

if [[ "$PROJN" == "ONE" ||  "$PROJN" == "two" ]] ; then ESC=hello_linux ; fi

The "postfix" ifis possible in Perl (and maybe somewhere else, too), but not in bash.

“后缀”if在 Perl 中是可能的(也可能在其他地方),但在 bash 中不行。

回答by Charles Duffy

The following syntax is a terser alternative:

以下语法是更简洁的替代方案:

[[ $PROJN = ONE || $PROJN = TWO ]] && ESC=hello_linux

...and the following is still shorter, and compliant with older shells:

...以下内容更短,并且与旧壳兼容:

case $PROJN in ONE|TWO) ESC=hello_linux ;; esac

回答by David W.

There's no problem using an ifinside a case statement. It's that your ifstatements are a wee bit incorrect.

使用ifinside case 语句没有问题。那是你的if陈述有点不正确。

You had your ifstatement on the same line as your $ESCassignment. Doesn't work outside of a casedoesn't work inside. Also, you need to use -ofor the orin your ifstatements, and you need AT LEAST one line to execute if your ifstatement is true. (I just put an echoas a place holder).

你的if陈述和你的$ESC作业在同一行。在外面case不起作用,在里面不起作用。此外,您需要在语句中使用-oforif,并且如果您的if语句为真,则至少需要执行一行。(我只是把一个echo作为占位符)。

It could be that this particular ifstatement should be outside of your case. I notice that they're all the same. No need to duplicate code in that case, just put your ifafter the esac.

可能是这个特殊的if语句应该在你的case. 我注意到它们都是一样的。在这种情况下无需重复代码,只需将您ifesac.

By the way: You can do an orinside of an if in either one of these two ways:

顺便说一句:您可以通过or以下两种方式之一进行 if的内部操作:

if [[ "$PROJN" == "ONE" -o  "$PROJN" == "two" ]]

or

或者

if [[ "$PROJN" == "ONE" ]] || [[  "$PROJN" == "two" ]]

And now back your regularly scheduled program...

现在支持你定期安排的节目......

findinfo() {
    OPT1=
    case  "$OPT1" in
       linux)
            echo "Setting environment"
            ESC="hello_linux" 
            if [[ "$PROJN" == "ONE" -o  "$PROJN" == "two" ]]
            then
                 echo "Here be dragons..."
            fi
            ;;
       Windows)
            echo "Setting environment"
            ESC="hello_windows" 
            if [[ "$PROJN" == "ONE" -o  "$PROJN" == "two" ]]
            then
                 echo "Here be dragons..."
            fi
            ;;
       Android)
            echo "Setting environment"
            ESC="hello_android" 
            if [[ "$PROJN" == "ONE" -o "$PROJN" == "two" ]]
            then
                 echo "Here be dragons..."
            fi
            ;;
    esac
}