4

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
3
  • 2
    exit is defined as exiting the shell. That's what it's defined to do. Why would you expect it to ever be capable of doing anything different? Commented Jun 16, 2015 at 17:10
  • 1
    Now, there's a break command to perform an early exit from loops, but you can't use that to break out of an if without wrapping it inside a loop. Commented Jun 16, 2015 at 17:11
  • in zsh, return works inside if-statements Commented Feb 14, 2022 at 20:02

3 Answers 3

11

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."
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.