if, elif, else statement issues in Bash

后端 未结 5 2174
忘了有多久
忘了有多久 2020-11-28 01:25

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 pr

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 01:51

    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
    

提交回复
热议问题