What's wrong with elsif in BASH?

markpatterson

Registered
Hi,

I was trying to write a bash script taking the argument start or stop. I read up at this tutorial
http://pegasus.rutgers.edu/~elflord/unix/bash-tute.html
and it appears there is an elsif, but it doesn't work for me. I have had to do separate if statements.

Here is (most of) the offending code:

Code:
#!/bin/bash
if [ $1 = "start" ] ; then
  echo "starting MySQL"
  /Library/StartupItems/MySQL/MySQL start
  echo "starting Apache"
  apachectl start
# fi  
# if [ $1 = "stop" ] ; then
elsif [$1 = "stop" ]  ; then
  echo "shutting down MySQL"
  /Library/StartupItems/MySQL/MySQL stop
  echo "shutting down Apache"
  apachectl stop
fi

Invoking it looks like this:

Pattersons-eMac:~ root# lamp.sh stop
/Users/mark/Dev/scripts/lamp.sh: line 20: syntax error near unexpected token `then'
/Users/mark/Dev/scripts/lamp.sh: line 20: `elsif [$1 = "stop" ] ; then'


If I remove the elsif line and uncomment the commented out lines, it works.

If this a bash versions thing?

Regards,

Mark
 
Thanks for that. I tried out elif and it worked. I told the author of the tutorial and he acknowledged the error.

Here's the full script, now that it seems to work perfectly is as follows. It needs to be run as root: i.e. do sudo bash first. I have added to $PATH in my ~/.bash_profile the directory I put the script in, ~/Dev/scripts, to access the script with any current directory, so it ends up looking like this:
PATH="${PATH}:/usr/local/bin:/usr/local/mysql/bin:~/Dev/scripts"

Code:
#!/bin/bash

usage ()
{
  echo "Usage: $0 [start|stop]"
  exit 1
}

# Suppress problem if no option
if [ -z $1 ] ; then
  usage
fi
if [ $1 = "start" ] ; then
  echo "starting MySQL"
  /Library/StartupItems/MySQL/MySQL start
  echo "starting Apache"
  apachectl start
elif [ $1 = "stop" ] ; then
  echo "shutting down MySQL"
  /Library/StartupItems/MySQL/MySQL stop
  echo "shutting down Apache"
  apachectl stop
else
  usage  
fi  
exit 0
 
Back
Top