如何在不退出程序的情况下退出 bash 中的 if 语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30873858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to exit if statement in bash without exiting program?
提问by m81
Rewriting this question to avoid more downvotes, since it's too late for me to delete it:
重写这个问题以避免更多的downvotes,因为我删除它为时已晚:
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:
我正在编写一个脚本,要求用户在sourcing
其他一些脚本之前进行确认。为了简化代码,假设有两个脚本可能是sourced
,但我希望用户source
不使用或只使用其中一个脚本 - 不能同时使用。我试图使用if true source-script else exit
无法工作的表单if
语句,因为我会退出该语句,但也会退出整个脚本,并且没有机会进行必要的清理。最初,我的脚本如下所示:
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:
@charles-duffy 提供了答案 - 只需将提示包装在一个函数中。就像是:
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
回答by Charles Duffy
First: Don't do any of this.Structure your program some other way. If you described to us whyyou 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:
回到问题:如果将块包装在循环中,则可以使用break
提前退出:
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:
或者,您可以使用一个函数,并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."
回答by Michael Rice
Just remove the exit
. It will print started if statement then print hello.
只需删除exit
. 它将打印开始的 if 语句然后打印你好。
回答by Rakholiya Jenish
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.
为什么要打印exit
. 如果你想退出循环,只需删除exit
它下面的所有代码(如果存在),因为无论哪种方式它都不会运行。
In case you are planning to use loop and want to exit the loop, use break
to exit the loop.
如果您打算使用循环并想退出循环,请使用break
退出循环。