4

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!

2
  • 2
    Why don't you use the init script provided with tomcat to start and stop it ?
    – jon_d
    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
4

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
0
3

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
}
2
  • 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
1

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.

2
  • '=' 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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