Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

STDOUT of shell script as input to perl script 2

Status
Not open for further replies.

nfaber

Technical User
Oct 22, 2001
446
US
Greetings!

I would like to use the STDOUT of a shell script as an input to a perl script I am going to write. Is the best (or maybe only) was to do it somethibg like, from the command line:

>>myperlscript.pl < myshellscript.sh


Or do I have to do something like have the shell script write to a file and have my perl script open the file up.

Thanks,

Nick
 
When you write the Perl script, just make sure you have a line that looks like:

Code:
my @shell_output = system(myshellscript.sh);

The output from the shell script will be loaded to the @shell_output array for you to manipulate.

- Rieekan
 
Thanks Rieekan, that would do it. So

>>myperlscript.pl < myshellscript.sh


would not work?
 
backticks is what you want.
@shell_output = `myshellscript.sh`;

the system functions returns the progam's exit status not it's output. for the output you want to use backticks.

Mid Ohio Valley Linux Users Group
- Geek Forum Rocks!
 
Won't this work:
myperlscript.pl < myshellscript.sh

if you read from STDIN in myperlscript.pl?

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
TomThumbKP is right, you can feed the output of any program to a perl script like that. The system() example won't work in fact.

In the perl script you would have something like this:

while(<>){
# do something with $_
print $_;
}


Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
What you want is
Code:
 myshellscript.pl | myperlscript.sh
with a '|' (pipe) pipe and not a '<' and with the shell script before the perl one.

The line
Code:
cmdA | cmdB
means that the output (STDOUT) of command cmdA is send to the input (STDIN) of command cmdB instead of the screen (or file if you use > fileA).

This is roughly equivalent to
Code:
cmdA > tmpfile;
cmdB < tmpfile;
rm tmpfile;
but without the file and with both commands running simultaneously and not sequentially, the output of cmdA being processed by cmdB as soon as available.

Of course in the perl script you have to read from STDIN as in MikeLacey example.

--------------------

Denis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top