假设以下test-pipefail.sh批处理:
#!/usr/bin/env bash
set -eo pipefail
./echoer.sh | head -n1 >/dev/null
echo "Might work with pipefail"
for i in {1..100} ; do
./echoer.sh | head -n1 >/dev/null
done
echo "Stable work with pipefail"
Run Code Online (Sandbox Code Playgroud)
使用echoer.sh内容:
#!/usr/bin/env bash
echo 'head (GNU coreutils) 8.30'
echo 'GNU bash, version 5.0.16(1)-release (x86_64-pc-linux-gnu)'
exit 0
Run Code Online (Sandbox Code Playgroud)
./test-pipefail.sh 的预期结果:
Might work with pipefail
Stable work with pipefail
Run Code Online (Sandbox Code Playgroud)
实际行为:
Might work with pipefail
Run Code Online (Sandbox Code Playgroud)
或(随机)没有输出。
如果我使用任何二进制实用程序而不是echoer.sh,管道中的 Writer 程序永远不会失败,但如果 writer 是 shell 脚本(例如,来自 glibc …