Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-15-2005, 05:11 AM   #1
LQ Newbie
Registered: Jul 2005
Posts: 2

Rep: Reputation: 0
Question Stdout And Stdin: Can't get it to do it "on the fly"...

I have this very annoying problem with STDIN and STDOUT... But first let me explain my problem.

When I type: cat somefile | grep something
each line is processed, in other words I get a "live feed" from the "cat" command.

Now if I write a perl script which looks like this:

print <STDIN>;
and do the following I would expect it to "live feed" my script with the STDOUT

cat somefile |

What happens is that the "cat" command store the STDOUT in the buffer and when the cat program is done it sends the STDOUT buffer to my script.

So my question is; is there a way to force linux (or the program) to send the STDOUT in a "live feed" alike manner?
Old 07-15-2005, 12:16 PM   #2
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
Looks like a Perl issue. The function 'print' establishes array context for the expression '<STDIN>'. Your code requests that all lines from STDIN are read first, and then the resulting array is passed to print. Obviously evaluation of '<STDIN>' has to wait till it's sure no more lines are coming in, i.e., till EOF.

You might want to process each line as soon as it arrives -- the 'while (<>)' construct achieves this. Compare
perl -e 'print <STDIN>'
perl -e 'print while (<STDIN>)'
Type a few lines to each, then C-d to signal end-of-file. (BTW, that space between '>' and ')' was inserted courtesy of LinuxQuestions )
Old 07-18-2005, 03:21 AM   #3
LQ Newbie
Registered: Jul 2005
Posts: 2

Original Poster
Rep: Reputation: 0
Looks like I'm trying to do the almost impossible.
Cause it looks like that perl does handle the STDIN on the fly, because the following is processed:

$ find /* |
print while(<>);
So the problem is the program that send the STDOUT, I think...

What I wanted to do was:

$ program |

Then make the feed a mysql db with the output without having to start the program more than once, but looks like I have to make the program auto TERM and then make a cron job or something to get it to work.

But still thanks for all your help.
Old 07-18-2005, 11:52 AM   #4
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,325

Rep: Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760
Anything is possible...

How do you know that 'program' use STDOUT,
it may use STDERR instead to produce output
Old 07-19-2005, 01:41 PM   #5
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31

Can you tell us some more about your goal? What happens when you run 'program |'? What would you want to happen? When you say "the following is processed", does it work as intended, or not?

Regarding your original question, if you run
cat somefile | perl -we 'print <STDIN>'
indeed the <> Perl construct does gather all the lines. But how can you tell the difference -- you get all of "somefile" anyway? Just the delay, if it's a long file?

To see if 'program' writes to STDERR, you could run 'program > out'. What you see was STDERR, what goes into 'out' was STDOUT.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
stdout stdin Furlinastis Linux - Newbie 3 08-12-2005 12:00 AM
Hard drive lose dma "on the fly" Hammett Linux - Hardware 4 08-23-2004 02:38 PM
redirect stdin/stdout of running process xtravar Linux - Software 3 03-06-2004 08:54 PM
Changing PS1 "on the fly" for root gandalf2041 Linux - General 1 11-22-2003 02:37 PM
stdin -> port -> stdout acid_kewpie Linux - Networking 5 04-12-2002 07:01 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:53 AM.

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