如何在不退出程序的情况下退出bash中的if语句? [英] 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:
我正在编写一个脚本,要求用户进行确认,并在 sourcing
之前添加一些其他脚本.为了简化代码,假设有两个可能是 source
的脚本,但是我希望用户不使用 source
或只使用其中一个脚本-不能同时使用.我试图使用 if真正的源脚本否则退出
形式的语句,该语句不起作用,因为我将退出 if
语句,而且还退出了脚本整体而言,将没有机会进行必要的清理.最初,我的脚本看起来像这样:
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提供了答案-只需将提示包装在函数中即可.像这样:
@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
推荐答案
首先:请勿执行任何操作.以其他方式构造程序.如果您向我们描述了为什么,您认为您需要这种行为,那么我们可能会告诉您如何实现此目的.
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.
深入探讨问题:如果将块包装成一个循环,则可以使用 break
提前退出:
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"
或者,您可以使用函数,然后 return
提前退出:
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."
这篇关于如何在不退出程序的情况下退出bash中的if语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!