如何有效地检查任意命令是否在 POSIX shell 中产生任何输出?(比方说git ls-files --killed
。)
有三种明显的方式,但在我看来,每一种都丑陋或邪恶:
- [ -n "$(git ls-files --killed)" ] - 命令的完整输出必须在内存中捕获并传递给测试(至少希望是内置的)
- [ "$(git ls-files --killed | wc -c)" -gt 0 ] - 涉及两个 fork- exec
- TMP=$(临时文件); git ls-files --killed >"$tempfile"; [ -s "$tempfile" ] && ...; rm "$tempfile" - 需要一个中间临时文件(同样也捕获所有输出)