简体   繁体   English

linux bash 和 grep 错误?

[英]linux bash and grep bug?

Doing the following:执行以下操作:

First console第一个控制台

touch /tmp/test

Second console第二个控制台

tail -f /tmp/test |grep propo |grep -v miles

Third console第三个控制台

echo propo >> /tmp/test

Second console must show "propo" but it doesn't shows anything, if you run in second console instead:第二个控制台必须显示“propo”,但它不显示任何内容,如果您在第二个控制台中运行:

tail -f /tmp/test |grep propo

And do echo propo >> /tmp/test it will show propo, but the grep -v is for miles not for propo并且做echo propo >> /tmp/test它会显示propo,但是grep -v是英里而不是propo

Why?为什么?

Test into your own environment if you want, it's pretty obvious but not working.如果您愿意,可以在您自己的环境中进行测试,这很明显但不起作用。

Why?为什么?

Most probably because the output of a command when piped to another command is fully buffered, not line buffered.很可能是因为一个命令的输出在通过管道传输到另一个命令时是完全缓冲的,而不是行缓冲的。 The output could be buffered in the first pipe or by grep.输出可以在第一个管道中或通过 grep 进行缓冲。

Use stdbuf -oL to force line buffering and grep --line-buffered for line buffered grep.使用stdbuf -oL强制行缓冲和grep --line-buffered用于行缓冲 grep。

the problem is that grep does not use line buffering by default;问题是 grep 默认不使用行缓冲; so the output will be buffered.所以输出将被缓冲。 You could use grep --line-buffered :您可以使用grep --line-buffered

tail -f /tmp/test | grep --line-buffered propo | grep -v miles

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM