bash - 从另一个脚本获取 shell 脚本并检查返回码

标签 bash shell

我想从另一个脚本获取文件(例如 name.sh)并检查返回代码并确定它的错误代码。如果我使用

source name.sh

然后如果 name.sh 返回非零返回码我的主脚本停止运行但我需要决定退出代码天气继续或停止。

如果我用

ret_code="`source name.sh`"
echo $ret_code

然后 ret_code 为 null 并且不打印错误代码。 我不能使用这些:

sh name.sh
./name.sh
bash name.sh

因为name.sh是不可执行的,我不希望它是可执行的

最佳答案

文件不需要可执行即可运行 sh name.sh。比使用 $?

sh name.sh
ret_code=$?

关于bash - 从另一个脚本获取 shell 脚本并检查返回码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16669212/

相关文章:

linux - Jenkins 管道中的多行 bash 命令

linux - 为目录中的所有文件生成MD5sum,并在另一个目录中获取它们的匹配项

Bash 目录中的 cat 文件到一个文件中,在内容之前插入文件名

bash - 在 bash 中启动 n 个子进程并等待一切完成

linux - 创建文件并将其放入文件夹 - Bash 脚本

linux - 编写一个输出第 n 行的 shell 脚本

bash - 如何删除数组中的元素,然后在 Shell 脚本中移动数组?

Linux 对话框 : Tree-Like checklist?

linux - 我需要更改包含特定字符串的 unix 文件的行

shell 中的 java 类路径