加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 09:15:35 所属栏目:安全 来源:网络整理
导读:我使用一个脚本扩展使用bash源功能; #!/bin/bashsource someneatscriptthatendsprematurely.sh 我想能够从那个脚本返回,而不破坏主脚本。 使用退出断点的主脚本,返回只在函数中有效,并尝试$(退出1)似乎也不工作。 那么,是否可以返回一个sub-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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读