LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   shellscripting: pipes and output redirection (https://www.linuxquestions.org/questions/programming-9/shellscripting-pipes-and-output-redirection-827708/)

bluebox 08-21-2010 05:54 PM

shellscripting: pipes and output redirection
 
I'm puzzled.

Please see these simple commands:

Code:

# dbus-monitor --system >> /data/eject.txt
This one works as expected ... dbus-monitor never terminates and whenever it outputs new lines, they are appended to "/data/eject.txt".

Code:

# dbus-monitor --system | grep "Eject"
This one does work too. Any line containing the string "Eject" appears immediately in my shell window.

Code:

dbus-monitor --system | grep "Eject" >> /data/eject.txt
This one does not work as expected. To be more precisely, nothing happens. Nothing gets written to "/data/eject.txt", nothing gets written on the screen.

So I tried
Code:

dbus-monitor --system | grep "Eject" | tee -a /data/eject.txt
Same result, nothing happens ... neither in "/data/eject.txt" nor on my screen.

As I said ... I am puzzled.

I suspect, I missed something elementary here about pipes and redirected output ...

bigearsbilly 08-21-2010 06:00 PM

hmm,
looks OK.

first of all
check permissions, check disk usage.

colucix 08-21-2010 06:06 PM

Try to add the option --line-buffered to grep: it forces the output to flush one line at a time. Otherwise the streamed output is flushed after a long time/size and you can't see it in real time.

bluebox 08-21-2010 06:47 PM

Quote:

Originally Posted by colucix (Post 4073636)
add the option --line-buffered to grep

Yeah, this did do the trick!

Thanks for the exceptional fast help, especially as I would never have suspected good old grep to be the culprit.

And my trust in pipes and redirection is restored :-)


All times are GMT -5. The time now is 11:38 AM.