如何从bash函数中的ssh命令获取返回代码,该代码来自另一个脚本?

时间:2017-03-23 19:08:00

标签: bash shell ssh sh

我有一个bash脚本,其中包含一些如下所示的函数:

my_functions.sh:

do_remote_command () {
    ssh user@hostname "./runScript.sh $var1 $var2"
}

我正在通过另一个脚本调用此函数,该脚本正如上面所示:

#!/bin/bash
source my_functions.sh
do_remote_command

我想从./runScript.sh获取返回码。我该如何说,"给我底层命令的返回码"?

1 个答案:

答案 0 :(得分:0)

将my_functions.sh修改为

do_remote_command () {
    ssh user@hostname "./runScript.sh $var1 $var2"
    return $?
}

...并且新脚本可以将返回值回显为

#!/bin/bash
source my_functions.sh
echo do_remote_command