LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-21-2010, 05:54 PM   #1
bluebox
Member
 
Registered: Jun 2004
Posts: 71

Rep: Reputation: 9
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 ...
 
Old 08-21-2010, 06:00 PM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
hmm,
looks OK.

first of all
check permissions, check disk usage.
 
Old 08-21-2010, 06:06 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 08-21-2010, 06:47 PM   #4
bluebox
Member
 
Registered: Jun 2004
Posts: 71

Original Poster
Rep: Reputation: 9
Quote:
Originally Posted by colucix View Post
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 :-)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
examples with 2 pipes+fork+handling error,output in different files nagendrar Programming 4 11-06-2009 06:36 AM
Output Redirection - Trying to output to screen and file? helptonewbie Linux - Newbie 7 03-19-2009 07:05 AM
LXer: Using Bash To Feed Command Output To A While Loop Without Using Pipes! LXer Syndicated Linux News 0 08-06-2008 12:10 PM
Sed gives output in pipes only when buffer is full jahvascriptmaniac Linux - Software 2 08-31-2006 05:57 PM
Output redirection rmartine Linux - Newbie 2 01-25-2005 06:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration