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
Bash does not allow if statement inside case statement
提问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, if
must precede the then
part:
在 bash 中,if
必须在then
部分之前:
if [[ "$PROJN" == "ONE" || "$PROJN" == "two" ]] ; then ESC=hello_linux ; fi
The "postfix" if
is 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 if
inside a case statement. It's that your if
statements are a wee bit incorrect.
使用if
inside case 语句没有问题。那是你的if
陈述有点不正确。
You had your if
statement on the same line as your $ESC
assignment. Doesn't work outside of a case
doesn't work inside. Also, you need to use -o
for the orin your if
statements, and you need AT LEAST one line to execute if your if
statement is true. (I just put an echo
as a place holder).
你的if
陈述和你的$ESC
作业在同一行。在外面case
不起作用,在里面不起作用。此外,您需要在语句中使用-o
for或if
,并且如果您的if
语句为真,则至少需要执行一行。(我只是把一个echo
作为占位符)。
It could be that this particular if
statement 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 if
after the esac
.
可能是这个特殊的if
语句应该在你的case
. 我注意到它们都是一样的。在这种情况下无需重复代码,只需将您if
的esac
.
By the way: You can do an or
inside 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
}