多个Unix管道不起作用 [英] Multiple unix pipes not working

查看:96
本文介绍了多个Unix管道不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个管道可以正常工作(打印"c"):

This first pipeline works fine (printing "c"):

echo "a" | sed 's/a/b/' | sed 's/b/c/'

这个人没有达到我的期望(当我在我的fifo中输入"a"时,什么都不会打印出来):

This one does not do what I expect (nothing gets printed when I feed an "a" into my fifo ):

mkfifo fifo;
cat fifo | sed 's/a/b/' | sed 's/b/c/' 

但是,如果我从后面的管道中删除第二个"sed"命令,则确实会显示"b".我认为我对管道和重定向的理解必须过于简单. 有人可以向我解释如何解决第二种情况,以便我可以对fifo的内容运行两个连续的命令吗?

However, if I remove the second "sed" command from the latter pipeline, I do get a "b" printed. I think my understanding of pipes and redirects must be too simplistic. Can someone explain to me how to fix the 2nd case so that I can run two successive commands on the contents of the fifo?

(请注意,这不是特定于fifo的问题,netcat也会发生相同的行为.我发布了有关netcat的类似问题,但没有得到答案)

(note this isn't a problem specific to fifo, the same behavior occurs with netcat too. I posted a similar question about netcat but got no answers)

推荐答案

缓冲. sed会根据输出是否为tty来更改其缓冲.当您有两个sed时,第一个确定它的输出不是tty,因此它正在缓冲.因此,当您拥有:

Buffering. sed is changing it's buffering depending on whether the output is a tty or not. When you have two sed's, the first determines it's output is not a tty so it is buffering. So when you have:

cat fifo | sed 's/a/b'

sed不会缓冲,因为它的输出是到tty的,因此您可以看到数据,但是当您拥有:

sed is not buffering as it's output is to a tty so you see the data but when you have:

cat fifo | sed 's/a/b' | sed 's/c/d'

第一个sed正在缓冲数据.根据您所运行的特定sed,有不同的方法来禁用缓冲. GNU sed具有--unbuffered选项,而BSD sed具有-l选项可切换到行缓冲.

the first sed is buffering the data. Depending on the specific sed you are running, there are different ways to disable buffering. GNU sed has the --unbuffered option while BSD sed has the -l option to switch to line buffering.

这篇关于多个Unix管道不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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