本文翻译自:if, elif, else statement issues in Bash
I can't seem to work out what the issue with the following if
statement is in regards to the elif
and then
. 我似乎无法弄清楚以下if
语句的问题是关于elif
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. 请记住, 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
#1楼
参考:https://stackoom.com/question/15HMz/如果-elif-其他声明在Bash中发布
#2楼
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 is a 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
. 你没有看到像elif[: command not found
(或类似)之类的东西的原因是,在看到if
then
,shell正在寻找elif
, else
或fi
。 However it finds another then
(after the mis-formatted elif[
). 然而它找到另一then
(误格式化后elif[
)。 Only after having parsed the statement it would be executed (and an error message like elif[: command not found
would be output). 只有在解析了语句之后才会执行它(并且会输出像elif[: command not found
的错误消息)。
#3楼
[
is a command. [
是一个命令。 It must be separated by whitespace from the preceding statement: 它必须用前面语句中的空格分隔:
elif [
#4楼
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
#5楼
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 aa 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