本文翻譯自: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