m81
m81

Reputation: 2317

How to exit if statement in bash without exiting program?

Rewriting this question to avoid more downvotes, since it's too late for me to delete it:

I'm writing a script that asks a user for confirmation and before sourcing some other scripts. To simplify the code, imagine that there are two scripts which might be sourced, but I want the user to either source none or exactly one of the scripts - not both. I was trying to use a statement of the form if true source-script else exit which wasn't working because I'd exit the if statement, but also the script as a whole, and wouldn't have a chance to do necessary cleanup. Originally, my script looked something like this:

echo "This script might do something terrible to your computer."
read -p "Do you wish to continue? (y/[n]) " -n 1;
echo
if ! [[ $REPLY =~ ^[Yy]$ ]]
then
    source "terrible_script.sh"
    # want some way to ensure that we don't prompt the user about the next script
    # don't want to just exit if the response is 'n' because we have to do cleanup
fi

echo "This script might do something really good to your computer."
read -p "Do you wish to continue? (y/[n]) " -n 1;
echo
if ! [[ $REPLY =~ ^[Yy]$ ]]
then
    source "good_script.sh"
fi

# do cleanup here
# regardless of whether or not any scripts were sourced

@charles-duffy provided the answer - simply wrap the prompts in a function. Something like:

function badscript() {
    echo "This script might do something terrible to your computer."
    read -p "Do you wish to continue? (y/[n]) " -n 1;
    echo
    if ! [[ $REPLY =~ ^[Yy]$ ]]
    then
        source "terrible_script.sh"
        return 0
    fi
}

function goodscript() {
    echo "This script might do something really good to your computer."
    read -p "Do you wish to continue? (y/[n]) " -n 1;
    echo
    if ! [[ $REPLY =~ ^[Yy]$ ]]
    then
        source "good_script.sh"
    fi
}

if ! badscript
then
    goodscript
fi

# cleanup code here

Upvotes: 4

Views: 53545

Answers (3)

Charles Duffy
Charles Duffy

Reputation: 295291

First: Don't do any of this. Structure your program some other way. If you described to us why you think you need this behavior, we could potentially have told you how to achieve it otherwise.


Getting down to the question: If you wrap your block in a loop, you can use break to exit early:

for _ in once; do
  if true; then
    echo "in the loop"
    break
    echo "not reached"
  fi
done
echo "this is reached"

Alternately, you can use a function, and return to exit early:

myfunc() {
  if true; then
    echo "in the loop"
    return
  fi
  echo "unreached"
}
myfunc
echo "this is reached"

Alternately, you can wrap your loop in a subshell (though this will prevent it from doing other things, like variable assignments that impact code outside the subshell, as well):

(if true; then
   echo "in the block"
   exit
   echo "unreached"
 fi)
echo "this is reached."

Upvotes: 11

Rakholiya Jenish
Rakholiya Jenish

Reputation: 3223

Why you want to print exit. If you want to go out of loop just remove exit and all the code below it (if exist), since either way it is not going to run.

In case you are planning to use loop and want to exit the loop, use break to exit the loop.

Upvotes: 1

Michael Rice
Michael Rice

Reputation: 8194

Just remove the exit. It will print started if statement then print hello.

Upvotes: 0

Related Questions