1

I could not understand whats going wrong with below if else statements.

if [ "$technology" == "AN_EUD" ] then
        if [ "$temporal_unit" == "15min" ] && [ "$regional_unit" == "node" ]
        then
                parm="DEVICEID"
        elif [ "$temporal_unit" == "15min" ] && [ "$regional_unit" == "trunk" ]
        then
                parm="DEVICEID"
        else echo "Unkown parameter"
        fi
else [ "$technology" == "IP" ] then
        if [ "$temporal_unit" == "15min" ] && [ "$regional_unit" == "item" ]
        then
                parm="ME_NAME"
        elif [ "$temporal_unit" == "min" ] && [ "$regional_unit" == "item" ]
        then
                parm="DEST"
        else echo "Unknown parameter"
        fi
fi

keeping getting below error

my.sh: line 67: syntax error near unexpected token `else'
my.sh: line 67: ` else [ "$technology" == "IP" ] then'
2

2 Answers 2

5
if [ "$technology" == "AN_EUD" ] then
                                ^

You're missing a semicolon there. The way it's written, then is just a regular argument to the [ command, not a shell keyword. (The same way it's not a keyword in the command echo if then else fi.)

Bash complains about the later else being an unexpected token, because it's still looking for the then, and since the else is at the start of a command, it is recognized as a keyword. But it doesn't fit the syntax.

Shellcheck.net also notices that, it says "Use semicolon or linefeed before 'then' (or quote to make it literal)."

After you fix that, there's this:

else [ "$technology" == "IP" ] then

That should probably be elif and not else. Without the semicolon before the then, this is actually fine as far as the shell syntax is concerned, the [ "$technology" == "IP" ] then command is just part of the else-block. The test won't work because [ expects to see ] as the last argument, but the rest of the block is executed. If you add the semicolon, but leave the else as is, you should get an error for "unexpected keyword then".

The other issue in your tests is that while == works in Bash and ksh, it's not standard. The standard comparison operator in [ .. ] is =.

(Note, I didn't look at what you're actually doing, just the syntax and the error.)

0
3

@ilkkachu has already detailed the problems with your code, I'll just point out that you can make that a bit more concise with a case statement. Here, the three variables are just concatenated together, and the string matched against the known cases:

case "$technology:$temporal_unit:$regional_unit" in
  (AN_EUD:15min:node | AN_EUD:15min:trunk)
    parm=DEVICEID;;
  (IP:15min:item)
    parm=ME_NAME;;
  (IP:min:item)
    parm=DEST;;
  (*)
    echo >&2 Unknown parameter;;
esac

Since the patterns are fixed, any colons in the variables can't get mixed with the colons used as separators.

Note that one difference with your if/fi based statement is that an error will also be output if $technology is anything but IP or AN_EUD. You may need to adapt the code if that's not what you want.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .