我在下面有一个 bash 脚本,它基本上是做一些事情并在最后调用 diff
。我想知道 bash 是否以与 diff
相同的行为处理退出代码。由于这个脚本被另一个脚本调用,所以我想知道我是否需要做任何类型的错误处理来处理 diff
的退出代码然后退出?
#!/bin/bash
# this is test.sh
# do something
# last line of the file:
diff -r $RUN_DIR/testdata/actual_result $RUN_DIR/testdata/expected_result
这是另一个调用上面的脚本:
# this is run_test.sh
if /bin/bash "${dir}/test.sh"; then
echo "SUCCESS: ${dir}"
else
echo "FAILED: ${dir}"
# do not exit here, we continue with other unit tests
fi
最佳答案
如果该 diff
命令是该 bash 文件中最后执行的命令,那么是的,它的返回值将是 bash 脚本的返回值。
替代方法是通过 exit
命令返回一个显式返回值,例如113 号导出
。执行将始终在 exit
调用后终止,因此您可以更加灵活地...
要了解这些事情,最好开始阅读“手册页”:man bash
...
关于linux - bash shell脚本中的最后一行命令是否将退出代码传递给父级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32158570/