How to exit if statement in bash without exiting program?

送分小仙女□ 提交于 2019-12-12 09:20:03

问题


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

回答1:


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."



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/30873858/how-to-exit-if-statement-in-bash-without-exiting-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!