如何返回到源代码的bash脚本?

前端之家收集整理的这篇文章主要介绍了如何返回到源代码的bash脚本?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用一个脚本扩展使用bash源功能;
#!/bin/bash

source someneatscriptthatendsprematurely.sh

我想能够从那个脚本返回,而不破坏主脚本。

使用退出断点的主脚本,返回只在函数中有效,并尝试$(退出1)似乎也不工作。

那么,是否可以返回一个sub-bash脚本而不打破主bash?

任何帮助赞赏!

你需要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
原文链接:https://www.f2er.com/bash/389614.html

猜你在找的Bash相关文章