【发布时间】:2013-07-28 11:07:41
【问题描述】:
管道程序何时终止,谁控制这个终止过程?我读过Bash: why the pipe is terminated?,但它只部分涵盖了这个问题。在尝试自己回答这个问题后,我做了几个例子来看看他们的输出。还有这个我看不懂:(a.sh):
#!/bin/bash
echoerr() { echo $@ 1>&2; }
echoerr a.sh started
sleep 1
echo 1
sleep 1
echo 2
sleep 1
echo 3
sleep 1 # Line 1
echo 4 # Line 2
echoerr a.sh finished
我以./a.sh | head -3 运行它。输出是:
a.sh started
1
2
3
从输出中我了解到./a.sh 在读取前 3 行输入后以SIGPIPE 信号终止,因为不需要更多数据。但是,当我删除 either 第 1 行 either 第 2 行时,输出更改为以下内容:
a.sh started
1
2
3
a.sh finished
所以我的问题是:
- 终止管道程序的政策是什么?
- 为什么第 1 行会影响程序行为?
- 为什么第 2 行会影响程序行为?
【问题讨论】:
标签: bash pipe termination sigpipe