Grep, tee, and sleep doesn't play well together

(echo foo && sleep 1 && echo bar) | grep -P . | tee /dev/null

      

The above code is the smallest case of my problem I could think of. The expected behavior is that there foo

will be an echo, the second will pass, then there bar

will be an echo code.

What actually happens is that the second passes then foo

and bar

simultaneously. If you remove either the command grep

or tee

(or both, obviously) the correct behavior will occur. But together with them it is not so.

I am guessing this is some kind of buffering problem, but I don't know how to get around it. The actual script that is the case for me has been running for quite some time and I don't see any log messages until the very end. Halp !: (

+3


source to share


1 answer


Etan's comment prompted me to look at the grep man page and I found a flag --line-buffered

. Adding that fixed the problem



(echo foo && sleep 1 && echo bar) | grep --line-buffered -P . | tee /dev/null

      

+4


source







All Articles