我可以使用多少个管道有限制? [英] Is there a limit on how many pipes I can use?

查看:112
本文介绍了我可以使用多少个管道有限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我问了一个问题,然后才给ping添加前缀. (我的最后一个问题)使我留下了以下内容:

So I asked a question before that added a prefix to a ping. (My last questions) that left me with the following line:

ping 8.8.8.8 |  while read line; do echo "$(date): $line"; done | grep time=

这很好.我只有一个问题,我无法将其保存到文件中.我尝试了一个简单的重定向,就像这样:

And this works GREAT. I have only one problem I can not get this to save it in a file. I tried it with just a simple redirect like so:

ping 8.8.8.8 |  while read line; do echo "$(date): $line"; done | grep time= >> googleping

但是什么都没有保存在文件中...

But nothing gets saved in the file...

然后我尝试了这个:

ping 8.8.8.8 |  while read line; do echo "$(date): $line"; done | grep time= | tee -a googleping

使用tee可以将其打印在屏幕上,也可以将其保存在文件中……再也没有运气了.

with tee to print it on screen and also save it in the file... no luck again.

(但是尝试了echo hello | tee -a googleping,效果很好...)

(But tried echo hello | tee -a googleping and it worked fine...)

因此,然后我尝试了另一个while循环,如下所示:

So and then I tried another while loop like so:

ping 8.8.8.8 |  while read line; do echo "$(date): $line"; done | grep time= | while read line; do echo $line; echo $line >> googleping; done

再也没有运气了...

No luck again...

那么一行中可以包含多少个管道和重定向是否有限制?如果可以的话,有一种方法仍然可以实现我无法到达google时的日志记录目标(我只是用grep time=进行了测试,以得到可靠的输出,并且将使用grep -v time=来获取所有没有时间的行在其中不管有没有错误可能是

So is there a limit on how many pipes and redirects one line can have? And if so is there a way that I can still achive my goal of logging when I can't reach google (I just tested it with grep time= to have garuenteed output and will use grep -v time= to get all lines that have no time in them no matter waht the error may be)

最后,我想在mac终端中进行操作,但是我在ubuntu服务器和mac上尝试了该方法,但都不能使用上述任何方法.

So to add is that in the end I want to do in in the mac terminal, but I tried it on ubuntu server and a mac, and neither work with any of the methodes described above.

我希望有人能帮助我!

推荐答案

这与您使用的管道数量无关.由于输出缓冲,您不会立即看到tee的输出.当grep的输出要到达管道而不是终端时,它将被缓冲.如果您有足够的耐心,则一段时间后(刷新缓冲区后)将显示输出.

This is not related to the number of pipes you are using. You don't see output from tee immediately because of output buffering. When grep's ouput is going to a pipe instead of a terminal, it get's block buffered. If you have enough patience, the output will appear after a while (once the buffer get's flushed).

除非程序独自显式处理缓冲,否则此行为在libc中实现.您可以使用stdbuf命令来影响此行为:

This behaviour is implemented in the libc unless a program explicitly handles buffering on it's own. You can influence this behaviour using the stdbuf command:

ping 8.8.8.8 \
  |  while read line; do echo "$(date): $line"; done \
  | stdbuf -o0 grep time= \
  | tee -a googleping

我正在使用stdbuf -o0调用grep,这会将grep的输出缓冲区大小缩小到零长度.另外,您可以使用-oL产生行缓冲输出.

I'm calling grep using stdbuf -o0 which shrinks the output buffer size of grep to zero length. Alternatively you can use -oL which produces line buffered output.

旁注:stdbuf适用于您的示例.但是,如果您仔细阅读stdbuf的手册页,则会发现:

Sidenote: stdbuf works for your example. But if you read the man page of stdbuf carefully, you'll notice:

注意:如果COMMAND调整其标准流的缓冲("tee" 例如),那么它将覆盖由"std- buf'.另外,某些过滤器(例如"dd"和"cat"等)不使用流 I/O,因此不受"stdbuf"设置的影响.

NOTE: If COMMAND adjusts the buffering of its standard streams ('tee' does for e.g.) then that will override corresponding settings changed by 'std‐ buf'. Also some filters (like 'dd' and 'cat' etc.) don't use streams for I/O, and are thus unaffected by 'stdbuf' settings.

这就是我上面所说的. stdbuf仅适用于无法自行处理缓冲的程序. tee是这样的程序.这意味着如果您从tee进一步管道,则不能使用stdbuf.

That's what I told above. stdbuf works only with programs which doesn't handle buffering on their own. tee is such a program. Meaning if you further pipe from tee you cannot use stdbuf.

这篇关于我可以使用多少个管道有限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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