如何通过 gnu 并行转发命令 运行 的 return 值

how to forward the return value of a command run through gnu parallel

我有以下命令

find $build -name "tester_*" -type f -executable | parallel "valgrind --tool=memcheck --leak-check=full --show-reachable=yes --track-origins=yes --error-exitcode=1 {} &> $build/memcheck.{/}.log"

以上命令将运行 valgrind 并行用于我的项目构建目录中的所有 tester_ 可执行文件。

如果其中一个测试失败,所有测试的执行都将停止,失败测试的 return 值将导致并行失败。这个失败的 return 值因此可以被拦截并可能报告给用户。

问题是由于 运行 的测试数量,如果能通知我测试失败会很方便。使用“||” (或)在 bash 中可以与 echo 一起提供帮助,如下所示:

find $build -name "tester_*" -type f -executable | parallel "valgrind --tool=memcheck --leak-check=full --show-reachable=yes --track-origins=yes --error-exitcode=1 {} &> $build/memcheck.{/}.log || echo {} failed"

不幸的是,现在 echo 命令将 return 成功,并行将继续执行。即使 "echo" 将被执行,是否有可能以某种方式传播此错误?

您可以使用

yourCommand || { echo {} failed; exit 1; }

parallelbash 命令中。如果 yourCommand 失败,这将打印 ... failed 然后以状态 1 退出。

如果实际退出代码很重要,您必须存储退出代码。

yourCommand || { err=$?; echo {} failed; exit $err; }

对于 {}-n 或类似的东西替代的罕见情况,最好使用 printf '%s failed\n' {} 而不是 echo {} failed