bash 退出函数栈而不退出shell
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11141120/
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
exit function stack without exiting shell
提问by Adrian
I was writing a script and then came across a odd problem. If I'd source a script that contains a bunch of functions that may call an error function which outputs a string and then exits, it will exit my shell. I know why it does it. It is because a function call is in the same process space as the caller (at least it is in bash), so the exit within the function terminates the current process with the exit code provided. Example:
我正在写一个脚本,然后遇到了一个奇怪的问题。如果我提供一个包含一堆函数的脚本,这些函数可能会调用一个错误函数,该函数输出一个字符串然后退出,它将退出我的 shell。我知道它为什么这样做。这是因为函数调用与调用者在同一个进程空间中(至少它在 bash 中),所以函数内的退出会使用提供的退出代码终止当前进程。例子:
error()
{
echo
exit 1
}
fn()
{
if [ $# == 0 ]; then
error "Insufficient parameters."
fi
# do stuff
}
$ fn
Insufficient parameters.
[shell terminates]
So my question is, can I exit all functions in the function stack without terminating the current shell and without spawning a new subshell?
所以我的问题是,我可以退出函数堆栈中的所有函数而不终止当前 shell 并且不生成新的子 shell 吗?
Thanks
谢谢
采纳答案by Adrian
To exit the function stack without exiting shell one can use the command:
要在不退出 shell 的情况下退出函数堆栈,可以使用以下命令:
kill -INT $$
As pizzastated, this is like pressing Ctrl-C, which will stop the current script from running and drop you down to the command prompt.
正如比萨所说,这就像按 Ctrl-C,这将停止当前脚本的运行并将您下拉到命令提示符。
Note: the only reason I didn't select pizza's answer is because this was buried in his/her answer and not answered directly.
注意:我没有选择比萨的答案的唯一原因是因为这被埋在他/她的答案中而不是直接回答。
回答by pizza
you can do a
你可以做一个
exit() { return ;}
then
然后
source ./your_script
In answer to the skeptics, this only affect the current shell, it does not affect shells you spawn.
作为对怀疑论者的回答,这仅影响当前的外壳,不会影响您生成的外壳。
The more informative form can be
更多信息形式可以是
exit() {
local ans
local line
read -p "You really want to exit this? " line
ans=$(echo $line)
case "$ans" in
Y);;
y);;
*)kill -INT $$;;
esac
unset -f exit
exit
}
回答by l0b0
You'll need to add return
statements to each of your functions to check the return value of any functions they call in turn. Sourcing a file is like cutting and pasting the code into the current context, with the minor exception of variables like $BASH_SOURCE
.
您需要向return
每个函数添加语句以检查它们依次调用的任何函数的返回值。获取文件就像将代码剪切并粘贴到当前上下文中一样,除了$BASH_SOURCE
.
Alternatively you could define fn
as a shell script, so that exit
will do what you want (unless a fork is too expensive).
或者,您可以定义fn
为 shell 脚本,这样就可以满足exit
您的需求(除非 fork 太贵了)。
回答by Nahuel Fouilleul
using return
statement, but you need to add return after calling error
usingreturn
语句,但调用错误后需要添加 return
回答by Mark Reed
The shell doesn't really have an exception mechanism for rewinding through many function calls at once. You have to just check return values and manually return all the way down.
shell 并没有真正的异常机制来一次回滚许多函数调用。您只需检查返回值并手动返回即可。