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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not sure whats wrong capture stdout

Status
Not open for further replies.

perlnewbie9292

Programmer
Jul 15, 2009
25
US
Hello all I am not sure what the heck I am doing wrong. I've been looking at this for a few hours now and I think I need help figuring out what it is I am doing wrong. I am trying to capture the output (stdout) that is produced when I rum/call a java script within Perl. Right now when I run my Perl script all runs fine, except that the stdout goes to the screen and I want to save it to an array so I mail the results to myself. Can someone please give me an example either with the code that I have below or how do capture stdout.

Right now print "$_\n", for @cmd; just captures the actual @cmd command line argument that is being run the the output of the java script.

thanks for the help in advanced.

Code:
my @cmd = sprinf( "java -classpath '%s' %s '%s' '%s' %s 2>&1", $class, $mainC, $stepP, $dpah, $next );
system(@cmd) == 0 or die "system @cmd failed $?\n";

print "$_\n", for @cmd;
 
system() returns the exit code of the program you're running (usually 0, or nonzero on error).

Use backticks instead.

Code:
my $output = `ls -hal /`;
print "output: $output\n";

If the output goes to STDERR instead of STDOUT, it won't be captured. You can redirect it to STDOUT in this case.

Code:
my $output = `ls -hal 2>>&1';

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Thanks for the reply, but by changing the code the way you've shown only gets me the STDOUT output which is what I wanted. However I can no longer check the exit status as that is not being saved. Since before running the system command got me a result of 0 or whatever that exit status was.

Or using your example how do I:
1) capture stdout
2) capture the exit status code 0, 127, 128 etc and for error checking purposes?

Thanks again for the help I really appreciate it.
 
Hi

perlnewbie9292 said:
However I can no longer check the exit status as that is not being saved.
You can.
Perl:
[b]my[/b] [navy]$output[/navy] [teal]=[/teal] `ls [teal]-[/teal]hal [teal]/[/teal]`[teal];[/teal]
[b]die[/b] [green][i]'external command failed'[/i][/green] [b]unless[/b] $[teal]?==[/teal][purple]0[/purple][teal];[/teal]
[b]print[/b] [green][i]"output: $output\n"[/i][/green][teal];[/teal]
See [tt]man perlvar[/tt] for more.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top