gpt4 book ai didi

bash - 你如何返回到源 bash 脚本?

转载 作者:行者123 更新时间:2023-11-29 08:41:05 26 4
gpt4 key购买 nike

我在 bash 脚本中使用“source”,如下所示:

#!/bin/bash
source someneatscriptthatendsprematurely.sh

我想退出 someneatscriptthatendsprematurely.sh 脚本,而不退出主脚本。

感谢任何帮助!

最佳答案

您需要 return 语句:

return [n]

Causes a function to exit with the return value specified by n. If n is 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 either n or 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.

您可以使用以下两个脚本看到这一点:

script1.sh:
. script2.sh
echo hello again

script2.sh:
echo hello
return
echo goodbye

当您运行 script1.sh 时,您会看到:

hello
hello again

关于bash - 你如何返回到源 bash 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3666846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com