ChatGPT解决这个技术问题 Extra ChatGPT

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. 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.

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
I wonder why we need the then statement in if and elif but not in else, and also in general.
@w17t, because we need to separate condition from sequence.
@codeforester I don't see much logic on marking a 500K views question as a duplicate to one that has only 5K
Using some code auto-formatting tools might help you by automatically adding/removing spaces around the brackets. You can search for plugins for your editor.
for me , i never expected a semicolon at the end of 'if' statement followed by 'then'.Your question itself was the solution for me. Thanks a lot !!

P
PatchingMatching

There is a space missing between elif and [:

elif[ "$seconds" -gt 0 ]

should be

elif [ "$seconds" -gt 0 ]

All together, the syntax to follow is:

if [ conditions ]; then
   # Things
elif [ other_conditions ]; then
   # Other things
else
   # In case none of the above occurs
fi

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. However it finds another then (after the mis-formatted elif[). Only after having parsed the statement it would be executed (and an error message like elif[: command not found would be output).


The reason the brackets need spaces is because they are just shortcuts for actual programs (at least the first bracket, the second one is just syntactic sugar as I understand it). To make sense of it, see the actual manpage for left bracket: $ man [
Is this post not supposed to be closed as typo?
@zx8754 could have been, but not it became a canonical way to fix this error, which looks quite useful (360K views and counting).
Some of my colleagues don't understand the concept of a "space" or "coding style" so this might not be a typo.
[ is a kind of alias for the test command. This is why the blank character is required. if test "$seconds" -eq 0; then ... fi is equivalent to if [ "$seconds" -eq 0 ];then ... fi ]. @LeiYang man testis what you actually looking for
J
James Ray

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

Oddly enough, this was the only complete and simple bash "if-then-else" construct I easily found on stackexchange...thanks.
indentation is optional. the interpreter can (and should) be #!/bin/sh.
@Chinggis6 Total nonsense, the interpreter could be #!/bin/sh but doesn't have to.
@Camusensei it's not a total nonsense as sh can be used instead for higher compatibility (not all *nix distros has bash as the default shell (some have ksh or ash but most of them do depend on the standard sh to function). Moreover if bash's specific or advanced features is not being used in the script then 'sh' should be used as the interpreter (one more reason for it) as it can handle the script by itself.
@Chinggis6 True, bad choice of words on my side.
c
choroba

[ is a command (or a builtin in some shells). It must be separated by whitespace from the preceding statement:

elif [

It is superseeded by a builtin in bash, but your message is still correct. use type -a [ to see that.
It's also a binary which always seemed odd to me.
M
Mr. Weirdo

I would recommend you having a look at the basics of conditioning in 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.

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

J
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:

#!/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