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

Capture output of system() function

Status
Not open for further replies.

jbhis8up

Technical User
Joined
May 20, 2004
Messages
3
Location
US
I'm trying to write a script that will write the output of a system("<some command>") function to a text file, however, the file that gets written has 0 bytes. Please help.

-Josh
 
Josh,

make sure you capture both STDOUT and STDERR, like this

system('some_command > file.out 2>&1');

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

 
Or if you want to do any processing of the results before they get printed to the file, you can do:

Code:
my $results = `some_command`;
open OUTPUT, "> somefile.txt";
print OUTPUT $results;
close OUTPUT;

Those symbols around some_command are actually backticks, the apostrophe-looking symbol that's on the key with the ~ (tilde) symbol.
 
Or
my (@results) = `some_command`;

Then process the array to do whatever you need to do with the output.

[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]
 
rharsh and Tom are both right - as usual with Perl there's more than twelve ways to do most things. You still have to use the 2>&1 trick to capture *all* the output from an external program/script though.

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

 
Something else about the script I was trying to write is that I want perl to perform actions against a file I specify on the command line. Basically I type "perl script.pl <filename>" to run the script agains that file. One of the commands I run against this file does not produce output to the screen but when I run the command by itself on the command line and then redirect the output to a file (exapmle: "command > output.txt") the file is populated. I'm trying to do the same thing from within perl. I appreciate the help. Thanks.

-Josh
 
As far as capturing the output, does it matter if I'm on a Win32 platform (which I am) as opposed to a UNIX flavor operating system? I've seen the "2>&1" when writing scripts in UNIX, but I've never seen that used on a Windows platform.
 
Good point there :-) And I had to go check it as well - but yes, it does seem to work as per UNIX. (Perl 5.8.1 and WinXP here)

The script I tested this with is

system('dir mike.pl >cmd.out 2>&1');

which produced output of:

C:\Perl>type cmd.out
Volume in drive C has no label.
Volume Serial Number is F8B2-3FC8

Directory of C:\Perl

File Not Found

Without the 2>&1 the line File Not Found is missing.

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

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top