您如何返回到源 bash 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3666846/
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 do you return to a sourced bash script?
提问by codar
I use a script that extends using the bash source feature;
我使用了一个使用 bash 源功能扩展的脚本;
#!/bin/bash
source someneatscriptthatendsprematurely.sh
I would like to be able to return from that script, without breaking the main script.
我希望能够从该脚本返回,而不会破坏主脚本。
Using exit breaks the main script, return is only valid in functions and experimenting with $(exit 1) does not seem to work either.
使用 exit 会破坏主脚本, return 仅在函数中有效,并且尝试 $(exit 1) 似乎也不起作用。
So, is it possible to return in a sub-bash script without breaking the main bash?
那么,是否可以在不破坏主 bash 的情况下返回子 bash 脚本?
Any help appreciated!
任何帮助表示赞赏!
回答by paxdiablo
You need the returnstatement:
你需要这样的return声明:
return [n]Causes a function to exit with the return value specified by
n. Ifnis omitted, the return status is that of the last command executed in the function body. If used outside a function, but during execution of a script by the.(source) command, it causes the shell to stop executing that script and return eithernor the exit status of the last command executed within the script as the exit status of the script. If used outside a function and not during execution of a script by., the return status is false. Any command associated with the RETURN trap is executed before execution resumes after the function or script.
return [n]使函数以 指定的返回值退出
n。如果n省略,则返回状态为函数体中执行的最后一条命令的状态。如果在函数外部使用,但在.(源)命令执行脚本期间,它会导致 shell 停止执行该脚本并返回n脚本内执行的最后一个命令的退出状态作为脚本的退出状态. 如果在函数外部而不是在脚本执行期间使用 by.,则返回状态为 false。与 RETURN 陷阱关联的任何命令都会在函数或脚本之后恢复执行之前执行。
You can see this in action with the following two scripts:
您可以使用以下两个脚本查看此操作:
script1.sh:
. script2.sh
echo hello again
script2.sh:
echo hello
return
echo goodbye
When you run script1.sh, you see:
当你运行时script1.sh,你会看到:
hello
hello again
回答by Bart Sas
Is it important that you can change environment variables? Since otherwise you can just execute the script by executing it without source:
可以更改环境变量很重要吗?否则,您可以通过在没有源的情况下执行脚本来执行脚本:
someneatscriptthatendsprematurely.sh
回答by Kenichi Shibata
I had the same problem just now
我刚才遇到了同样的问题
I realized that adding a checker function and returning that will not also return the function on its caller for example.
例如,我意识到添加一个检查器函数并返回它也不会在其调用者上返回该函数。
On bash_functions
关于 bash_functions
function install_packer_linux() {
check_wget && check_unzip
wget https://releases.hashicorp.com/packer/1.1.2/packer_1.1.2_linux_amd64.zip
unzip packer_1.1.2_linux_amd64.zip
mv packer ~/.local/bin
rm -f packer_1.1.2_linux_amd64.zip
}
function check_unzip() {
if ! [ -x "$(command -v unzip)" ]; then
echo "Error: unzip is not installed"
return 1
else
return 0
fi
}
function check_wget() {
if ! [ -x "$(command -v wget)" ]; then
echo "Error!: wget is not installed"
return 1
else
return 0
fi
}
$ source ~/.bash_functions
What happens here is since the checkers is the only place its returned so install_packer_linux will still continue
这里发生的事情是因为跳棋是它返回的唯一地方,所以 install_packer_linux 仍然会继续
So you can do two things here. Either keep the current format (function calling another function) as is and evaluate using truthy value then return if the values are not truthy or rewrite the checker on the main installer_packer_linux function
所以你可以在这里做两件事。保持当前格式(函数调用另一个函数)并使用真值进行评估,如果值不真则返回,或者重写主 installer_packer_linux 函数上的检查器
Truthy:
真实:
function install_packer_linux() {
check_wget && check_unzip || return
wget https://releases.hashicorp.com/packer/1.1.2/packer_1.1.2_linux_amd64.zip
unzip packer_1.1.2_linux_amd64.zip
mv packer ~/.local/bin
rm -f packer_1.1.2_linux_amd64.zip
}
Notice we added || return after the checks and concatenated the checks using && so if not both checks are truthy we return the function
注意我们添加了 || 检查后返回并使用 && 连接检查,所以如果不是两个检查都为真,我们返回函数

