Misbehaving head with redirection

ε祈祈猫儿з 提交于 2019-11-28 03:34:01

问题


In a reply to Piping a file through tail and head via tee, a strange behaviour of head has been observed in the following construct when working with huge files:

#! /bin/bash
for i in {1..1000000} ; do echo $i ; done > /tmp/n

( tee >(sed -n '1,3p'        >&3 ) < /tmp/n | tail -n2 ) 3>&1 # Correct
echo '#'
( tee >(tac | tail -n3 | tac >&3 ) < /tmp/n | tail -n2 ) 3>&1 # Correct
echo '#'
( tee >(head -n3             >&3 ) < /tmp/n | tail -n2 ) 3>&1 # Not correct!?

Output:

1
2
3
999999
1000000
#
1
2
3
999999
1000000
#
1
2
3
15504
15

Question:

Why does not the last line output the same lines as the previous two lines?


回答1:


This is because head exits as soon as it transfers three first lines. Subsequently, tee gets killed with SIGPIPE because the reading end of the "FILE" pipe it is writing to is closed, but not until it manages to output some lines to its stdout.

If you execute just this:

tee >(head -n3 >/dev/null) < /tmp/n

You will see what happens better.

OTOH, tac reads the whole file as it has to reverse it, as does sed, probably to be consistent.



来源:https://stackoverflow.com/questions/16664257/misbehaving-head-with-redirection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!