Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have a bash script as below which installs zookeeper but only if not installed already.

##zookeper
installZook(){

        ZOOK_VERSION="3.4.5"
        ZOOK_TOOL="zookeeper-${ZOOK_VERSION}"
        ZOOK_DOWNLOAD_URL="http://www.us.apache.org/dist/zookeeper/${ZOOK_TOOL}/${ZOOK_TOOL}.tar.gz"

        if [ -e $DEFAULT_INSTALLATION_DEST/${ZOOK_TOOL} ]; then
                echo "${ZOOK_TOOL} alreay installed";
                exit 1;      # <<<< here
        elif [ ! -e $DEFAULT_SOURCE_ROOT/${ZOOK_TOOL}.tar.gz ]; then
                wgetIt $ZOOK_DOWNLOAD_URL
        else
                echo "[info] : $DEFAULT_SOURCE_ROOT/$ZOOK_TOOL already exists"
        fi

        sudo mkdir -p /var/lib/zookeeper
        sudo mkdir -p /var/log/zookeeper

        tarIt "$DEFAULT_SOURCE_ROOT/$ZOOK_TOOL.tar.gz"
        sudo chmod 777 -R $DEFAULT_INSTALLATION_DEST/$ZOOK_TOOL
        cp $DEFAULT_INSTALLATION_DEST/$ZOOK_TOOL/conf/zoo_sample.cfg $DEFAULT_INSTALLATION_DEST/$ZOOK_TOOL/conf/zoo.cfg
cat >> ~/.bash_profile <<'EOF'

  ###############################
  ########### ZOOK ###############
  ###############################
  ZOOK_HOME=/usr/local/zookeper-3.4.5
  export ZOOK_HOME
  export PATH=$PATH:$ZOOK_HOME/bin

EOF
}

At the line marked <<<< here, if zookeeper is already installed, what I want is to exit the script below it. But using exit exits the terminal itself.

share|improve this question
1  
How do you launch this script ? –  Benoit Jun 23 '14 at 13:39
    
use return 1 instead of exit 1 –  polym Jun 23 '14 at 13:39

2 Answers 2

up vote 3 down vote accepted

If launching a script with an exit statement in it, you have to launch it as a child of you current child.

If you launch it inside the current shell of started with your terminal session (using . ./<scriptname> any exit will close the main shell, the one started along your terminal session.

If you had launched your script like bash ./<scriptname> (or any other shell instead of bash), then exit would have stopped your child shell and not the one used by your terminal.

If your script has executable permissions, executing it directly without giving the name of the shell will execute it in a child shell too.

Using return instead of exit will allow you to still launch your script using . ./<script name> without closing the current shell. But you need to use return to exit from a function only or a sourced script (script ran using the . ./<scriptname> syntax).

share|improve this answer

Instead of using exit 1 try using return 1.

share|improve this answer
    
return will return from the InstallZook() function, not exit the script. –  Barmar Jun 23 '14 at 15:00

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.