Ponyo Ponyo
Ponyo Ponyo

Reputation: 9

Trying to compare characters in shell script but I'm getting an error

Why isn't this if-else operation working? I fixed my spacing many times yet getting the error syntax error near unexpected token `elif' , I have tried using "==" instead of "=", I have tried using two third brackets but nothing seems to work.

if [ "$operation" = "+" ]
  then addition()
elif [ "$operation" = "-" ]
  then subtraction()
elif [ "$operation" = "*" ]
  then multiplication()
elif [ "$operation" = "/" ]
  then division()
fi

Upvotes: 0

Views: 230

Answers (1)

BarathVutukuri
BarathVutukuri

Reputation: 1303

You should remove parantheses to call the function. Whenever you encounter syntax issues like this, please use shellcheck website to identify what's wrong.

if [ "$operation" = "+" ]
  then addition
elif [ "$operation" = "-" ]
  then subtraction
elif [ "$operation" = "*" ]
  then multiplication
elif [ "$operation" = "/" ]
  then division
fi

Upvotes: 3

Related Questions