(可能与Do some programs not accept process substitution for input files有关?)
在一些 Bash 单元测试脚本中,我使用以下技巧来记录和显示命令的标准输出和标准错误:
command > >(tee "${stdoutF}") 2> >(tee "${stderrF}" >&2)
这个过程产生一些输出到标准输出,所以$stdoutF
文件得到一些数据。然后我运行另一个不输出任何数据的命令:
diff -r "$source" "$target" > >(tee "${stdoutF}") 2> >(tee "${stderrF}" >&2)
但是,在运行空性测试之前,这个过程并不总是成功完成(使用shunit-ng):
assertNull 'Unexpected output to stdout' "$(<"$stdoutF")"
在 100 次运行测试中,这失败了 25 次。
sync
在测试文件是否为空之前调用是否足够:
sync
assertNull 'Unexpected output to stdout' "$(<"$stdoutF")"
...和/或它是否应该通过强制命令的顺序来工作:
diff -r "$source" "$target" \
> >(tee "${stdoutF}"; assertNull 'Unexpected output to stdout' "$(<"$stdoutF")")
2> >(tee "${stderrF}" >&2)
...和/或是否有可能以tee
某种方式assertNull
直接而不是文件?
更新:sync
不是答案 - 请参阅下面 Gilles 的回复。
更新 2:进一步讨论Save stdout、stderr 和 stdout+stderr synchronously。感谢您的回答!