LinuxQuestions.org
Register a domain and help support LQ
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 07-15-2005, 04:11 AM   #1
Eskild
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:

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

cat somefile | myscript.pl

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, 11:16 AM   #2
Quigi
Member
 
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
Code:
perl -e 'print <STDIN>'
with
Code:
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, 02:21 AM   #3
Eskild
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 /* | myscript.pl
Code:
#myscript.pl
print while(<>);
So the problem is the program that send the STDOUT, I think...

What I wanted to do was:

$ program | myscript.pl

Then make the myscript.pl 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, 10:52 AM   #4
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,445

Rep: Reputation: 94
Anything is possible...

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

Rep: Reputation: 31
Eskild,

Can you tell us some more about your goal? What happens when you run 'program | myscript.pl'? 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
Code:
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.
 
  


Reply


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


All times are GMT -5. The time now is 03:23 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration