image
image

Go Back   macosx.com > Mac Help Forums > Unix & X11

Reply
 
Thread Tools
  #1  
Old July 21st, 2004, 03:23 AM
markpatterson's Avatar
Registered User
 
Join Date: Jul 2002
Location: Brisbane, Australia
Posts: 108
Thanks: 0
Thanked 0 Times in 0 Posts
markpatterson is on a distinguished road
Unhappy What's wrong with elsif in BASH?

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
Reply With Quote
  #2  
Old July 21st, 2004, 12:08 PM
scruffy's Avatar
Notorious Olive Counter
 
Join Date: Dec 2000
Location: Soviet Canuckistan
Posts: 1,726
Thanks: 0
Thanked 0 Times in 0 Posts
scruffy is on a distinguished road
it's "elif" not "elsif"
__________________

What is the robbing of a bank compared to the founding of a bank?
-- Bertold Brecht
Reply With Quote
  #3  
Old July 21st, 2004, 09:04 PM
markpatterson's Avatar
Registered User
 
Join Date: Jul 2002
Location: Brisbane, Australia
Posts: 108
Thanks: 0
Thanked 0 Times in 0 Posts
markpatterson is on a distinguished road
Thumbs up The author says he's fixed it

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

Last edited by markpatterson; July 21st, 2004 at 09:21 PM.
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT -5. The time now is 09:57 PM.


Mac Support® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2000-2008 DigitalCrowd, Inc.