行为不端的头重定向 [英] Misbehaving head with redirection

查看:164
本文介绍了行为不端的头重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在来通过配管三通通过尾巴和头文件的答复,一个奇怪的行为巨大文件时,已在下面的结构观察到的:

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
( tee >(tac | tail -n3 | tac >&3 ) < /tmp/n | tail -n2 ) 3>&1 # Correct
( tee >(head -n3             >&3 ) < /tmp/n | tail -n2 ) 3>&1 # Not correct!?

输出:

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

问:

为什么不是最后一行输出线和一样的previous两行?

Question:

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

推荐答案

这是因为尽快退出,因为它传递三个第一线。随后, T恤获取与SIGPIPE杀死,因为文件管它是写作的读数年底关闭,但直到它管理输出一些行到标准输出。

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.

如果你执行只是这:

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

您将看看会发生什么更好的。

You will see what happens better.

OTOH, TAC 读取整个文件,因为它有扭转它一样, SED ,大概是是一致的。

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

这篇关于行为不端的头重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆