你如何返回到源bash脚本? [英] How do you return to a sourced bash script?

查看:219
本文介绍了你如何返回到源bash脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用扩展使用bash的源功能的脚本;

I use a script that extends using the bash source feature;

#!/bin/bash

source someneatscriptthatendsprematurely.sh

我希望能够从该脚本返回,不破坏主脚本。

I would like to be able to return from that script, without breaking the main script.

使用出口打破了主脚本,回报仅在功能有效,并尝试$(1号出口)似乎不工作的。

Using exit breaks the main script, return is only valid in functions and experimenting with $(exit 1) does not seem to work either.

那么,是不是可以在子bash脚本没有打破主要的bash回来吗?

So, is it possible to return in a sub-bash script without breaking the main bash?

任何帮助AP preciated!

Any help appreciated!

推荐答案

您需要的收益语句:

回归[N] 结果
             导致一个函数指定的n返回值退出。如果省略n,返回状态是函数体最后执行的命令的。如果函数外使用,而是由一个脚本的执行过程中。 (source)命令,它会导致shell停止执行该脚本,返回n或脚本作为脚本的退出状态中执行的最后一个命令的退出状态。如果通过脚本执行期间使用的功能之外,没有,返回状态是假的。函数或脚本前后执行恢复执行与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

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

hello
hello again

这篇关于你如何返回到源bash脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆