Friday, February 26, 2016

Shell Script If Then Else logic

I was recently learning some shell scripting, so of course I only wanted some commands to trigger if a condition was met.
I wrote

if [condition] then
     echo "work please if"
else
  echo "work please else"
fi

but I kept getting an error on Terminal saying there was something wrong with my else, some unexpected else encountered.
So of course the problem was that if and then were on the same line, causing the if statement to be interpreted as a one-liner instead of a multi-liner.

Solution:
if [condition]
then
  echo "work please if"
else
  echo "work please else"
fi

I hope this helps some of you out there!