微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何返回到源代码的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.jb51.cc/bash/389614.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐