Bash 中的 if、elif、else 语句问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16034749/
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
if, elif, else statement issues in Bash
提问by StuStirling
I can't seem to work out what the issue with the following if
statement is in regards to the elif
and then
. Keep in mind the printf
is still under development I just haven't been able to test it yet in the statement so is more than likely wrong.
我似乎无法弄清楚以下if
语句的问题是关于elif
和then
。请记住,printf
它仍在开发中,我只是无法在声明中对其进行测试,因此很可能是错误的。
The error I'm getting is:
我得到的错误是:
./timezone_string.sh: line 14: syntax error near unexpected token `then'
./timezone_string.sh: line 14: `then'
And the statement is like so.
声明是这样的。
if [ "$seconds" -eq 0 ];then
$timezone_string="Z"
elif[ "$seconds" -gt 0 ]
then
$timezone_string=`printf "%02d:%02d" $seconds/3600 ($seconds/60)%60`
else
echo "Unknown parameter"
fi
回答by fedorqui 'SO stop harming'
There is a space missing between elif
and [
:
elif
和之间缺少一个空格[
:
elif[ "$seconds" -gt 0 ]
should be
应该
elif [ "$seconds" -gt 0 ]
As I see this question is getting a lot of views, it is important to indicate that the syntax to follow is:
正如我看到这个问题得到了很多观点,重要的是指出要遵循的语法是:
if [ conditions ]
# ^ ^ ^
meaning that spaces are needed around the brackets. Otherwise, it won't work. This is because [
itself isa command.
这意味着括号周围需要空格。否则,它不会工作。这是因为[
它本身就是一个命令。
The reason why you are not seeing something like elif[: command not found
(or similar) is that after seeing if
and then
, the shell is looking for either elif
, else
, or fi
. However it finds another then
(after the mis-formatted elif[
). Only afterhaving parsed the statement it would be executed (and an error message like elif[: command not found
would be output).
为什么你没有看到类似的原因elif[: command not found
(或类似)是在看到if
和then
,壳要么寻找elif
,else
或fi
。但是它找到了另一个then
(在格式错误之后elif[
)。只有在解析了语句之后,它才会被执行(并且elif[: command not found
会输出一条错误消息)。
回答by anubhava
You have some syntax issues with your script. Here is a fixed version:
您的脚本存在一些语法问题。这是一个固定版本:
#!/bin/bash
if [ "$seconds" -eq 0 ]; then
timezone_string="Z"
elif [ "$seconds" -gt 0 ]; then
timezone_string=$(printf "%02d:%02d" $((seconds/3600)) $(((seconds / 60) % 60)))
else
echo "Unknown parameter"
fi
回答by choroba
[
is a command (or a builtin in some shells). It must be separated by whitespace from the preceding statement:
[
是一个命令(或某些 shell 中的内置命令)。它必须与前面的语句用空格分隔:
elif [
回答by Mr. Weirdo
I would recommend you having a look at the basics of conditioning in bash.
我建议您查看 bash 中调节的基础知识。
The symbol "["is a command and must have a whitespace prior to it. If you don't give whitespace after your elif, the system interprets elif[as a a particular command which is definitely not what you'd want at this time.
符号“[”是一个命令,它前面必须有一个空格。如果您在 elif 后没有给出空格,系统会将elif[解释为一个特定的命令,这绝对不是您此时想要的。
Usage:
用法:
elif(A COMPULSORY WHITESPACE WITHOUT PARENTHESIS)[(A WHITE SPACE WITHOUT PARENTHESIS)conditions(A WHITESPACE WITHOUT PARENTHESIS)]
In short, edit your code segment to:
简而言之,将您的代码段编辑为:
elif [ "$seconds" -gt 0 ]
You'd be fine with no compilation errors. Your final code segment should look like this:
没有编译错误就可以了。您的最终代码段应如下所示:
#!/bin/sh
if [ "$seconds" -eq 0 ];then
$timezone_string="Z"
elif [ "$seconds" -gt 0 ]
then
$timezone_string=`printf "%02d:%02d" $seconds/3600 ($seconds/60)%60`
else
echo "Unknown parameter"
fi
回答by John Walsh
Missing space between elif
and [
rest your program is correct. you need to correct it an check it out. here is fixed program:
程序之间缺少空格elif
和[
休息是正确的。你需要更正它并检查它。这是固定程序:
#!/bin/bash
if [ "$seconds" -eq 0 ]; then
timezone_string="Z"
elif [ "$seconds" -gt 0 ]; then
timezone_string=$(printf "%02d:%02d" $((seconds/3600)) $(((seconds / 60) % 60)))
else
echo "Unknown parameter"
fi
useful link related to this bash if else statement
与此bash if else 语句相关的有用链接