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
then
statement in if
and elif
but not in else
, and also in general.
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).
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
bash
"if-then-else" construct I easily found on stackexchange...thanks.
#!/bin/sh
.
#!/bin/sh
but doesn't have to.
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.
[
is a command (or a builtin in some shells). It must be separated by whitespace from the preceding statement:
elif [
type -a [
to see that.
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
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
Success story sharing
$ man [
if test "$seconds" -eq 0; then ... fi
is equivalent toif [ "$seconds" -eq 0 ];then ... fi ]
. @LeiYangman test
is what you actually looking for