Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm new to linux (using Ubuntu 10.10) and I'd like to add a function to .bashrc that starts/stops a tomcat server. I'm trying to parse two options for the command "tomcat". I've tried the following in various permutations, but none of them work.

export CATALINA_HOME=/tomcat/directory

function tomcat {
       if [$1 = "-s" ]; then
          `sh $CATALINA_HOME/bin/startup.sh`
       elif [$1 = "-x"]; then
          `sh $CATALINA_HOME/bin/shutdown.sh`
       else
          echo "Enter '-s' to start Tomcat, '-x' to shutdown."
       fi
    }

Why doesn't this work? What's the right way to do it?

Thanks!

share|improve this question
1  
Why don't you use the init script provided with tomcat to start and stop it ? –  Fussy Salsify Feb 16 '11 at 17:08
1  
Why are the backquotes surrounding the commands? That will execute the startup and shutdown scripts but then try to run their output as another command, which will result in a strange error. –  daveadams Feb 16 '11 at 17:18
add comment

3 Answers

up vote 1 down vote accepted

Putting everything together here, I think this code will work for you:

export CATALINA_HOME=/tomcat/directory

function tomcat {
    if [ "$1" = "-s" ]; then
        sh $CATALINA_HOME/bin/startup.sh
    elif [ "$1" = "-x" ]; then
        sh $CATALINA_HOME/bin/shutdown.sh
    else
        echo "Enter '-s' to start Tomcat, '-x' to shutdown."
    fi
}

Important changes from your version:

  • Removed backquotes around startup and shutdown commands
  • Surrounded the $1 variable in double quotes

Some notes about bash syntax and safety:

  • function tomcat { is equivalent to tomcat() {
  • As @ooshro points out, a space is required after the [ after if -- when in doubt, space it out
  • in comparisons, you should prefer = over ==, as single-equals is the standard other shells use
  • always surround variables (like $1) in double quotes, especially in comparison statements, or anywhere where leaving them out would cause a problem
  • @ooshro's case syntax is a more concise way to deal with this problem, but it can be less flexible if you plan to make your code more complicated in the future
  • backquotes capture the output of a command and then replace it into the script, so if you surround an entire line with backquotes, bash will execute that line and then try to execute the output, which is rarely what you probably want
  • You don't need backquote functionality for this script, but when you do, I suggest using the $(...) syntax instead, as it's nestable and in my opinion far easier to read and understand
share|improve this answer
    
Thanks for pulling it all together Dave. Very helpful. –  Erasmus777 Feb 18 '11 at 19:55
add comment

Try(error in the lack of space after '['):

export CATALINA_HOME=/tomcat/directory

tomcat() {
        case "$1" in
            -s) ${CATALINA_HOME}/bin/startup.sh;;
            -x) ${CATALINA_HOME}/bin/shutdown.sh;;
            *) "Enter '-s' to start Tomcat, '-x' to shutdown."
        esac
}
share|improve this answer
    
Thanks! This is the one that worked for me. –  Erasmus777 Feb 16 '11 at 17:28
1  
Safest if you surround $1 with double-quotes in case the command is run without an argument. –  daveadams Feb 16 '11 at 17:29
    
@daveadams You are right –  ooshro Feb 16 '11 at 17:36
add comment

What do you mean with "does not work"? Do you get any error messages? What happens when you start your function?

Try putting a space always after and before the "[" and "]" .

Also quote $1 as "$1".

And "=" should be "==", so:

export CATALINA_HOME=/tomcat/directory

function tomcat {
   if [ "$1" == "-s" ]; then
      `sh $CATALINA_HOME/bin/startup.sh`
   elif [ "$1" == "-x" ]; then
      `sh $CATALINA_HOME/bin/shutdown.sh`
   else
      echo "Enter '-s' to start Tomcat, '-x' to shutdown."
   fi
}

works for me.

share|improve this answer
    
'=' equal '==' in conditional expressions –  ooshro Feb 16 '11 at 17:14
    
Yes, in bash conditionals '==' is allowed, but '=' is safer for compatibility with other shells. –  daveadams Feb 16 '11 at 17:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.