capturing output of executed system command 
Author Message
 capturing output of executed system command

suppose i have:

system 'ps'

how can i redirect the output into an array?

thanks,
benson



Sun, 20 Nov 2005 01:33:24 GMT  
 capturing output of executed system command
On Tue, 3 Jun 2003 17:33:24 -0700,

Quote:
> suppose i have:

> system 'ps'

> how can i redirect the output into an array?

You could try reading the documentation for the function you are using.

Possibly the bit that says "This is _not_ want you want to use to capture the
output from a cooment, for that you should use..." will be useful to read.

And of course before asking a question, checking the FAQ is the polite thing
to do:

perlfaq8:

Why can't I get the output of a command with system()?

--
Sam Holden



Sun, 20 Nov 2005 01:57:56 GMT  
 capturing output of executed system command

Quote:

> Possibly the bit that says "This is _not_ want you want to use to capture the
> output from a cooment, for that you should use..." will be useful to read.

                ^^^^^^^

Now that's quite a typo :)

--
Sam Holden



Sun, 20 Nov 2005 01:59:26 GMT  
 capturing output of executed system command

Quote:

> suppose i have:

> system 'ps'

> how can i redirect the output into an array?

> thanks,
> benson

This code should do it (adapt as needed.)

#!/usr/bin/perl

$ps=`ps`; # capture output of 'ps'

$lc=0;

  $lc++; # line count just to show that the string _has_ been split.
  print "$lc: $line\n"; # print one line from the array.
  };

The important bits are the `ps` in backticks - if you put any system
command in backticks, the resulting expression will evaluate as the
_output_ of that command (not exit code as with 'system'). Also the 'split
/\n/,$ps' which means split the string $ps into an array with one element
for each line of the string (splitting on the newline character). You
should be able to figure out how to split the individual lines into fields
using the same command. There might be a module to do this more robustly -
have a look on CPAN.

andy.

--
--------------------------
remove ' n - u - l - l ' to email me.



Sun, 20 Nov 2005 02:07:09 GMT  
 capturing output of executed system command

Quote:


>> system 'ps'

>> how can i redirect the output into an array?

[ snip sig, etc ]

Quote:
> $ps=`ps`; # capture output of 'ps'
> print $ps ."\n"; # print output as a string.


You're making this harder than it needs to be:


--
Steve



Sun, 20 Nov 2005 04:16:38 GMT  
 capturing output of executed system command
thanks. didn't know you could do this.

andy.

Quote:



>>> system 'ps'

>>> how can i redirect the output into an array?

> [ snip sig, etc ]

>> $ps=`ps`; # capture output of 'ps'
>> print $ps ."\n"; # print output as a string.

> You're making this harder than it needs to be:



--
--------------------------
remove ' n - u - l - l ' to email me.


Sun, 20 Nov 2005 04:36:24 GMT  
 capturing output of executed system command

Quote:

> system 'ps'

> how can i redirect the output into an array?

Folks that do not read the documentation for the function that
they are using deserve any pain they get.

Try to avoid pain.

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Sun, 20 Nov 2005 04:27:25 GMT  
 capturing output of executed system command

Quote:


>> suppose i have:

>> system 'ps'

>> how can i redirect the output into an array?
> $ps=`ps`; # capture output of 'ps'

> Also the 'split
> /\n/,$ps' which means split the string $ps into an array with one element
> for each line of the string

If you use backticks in list context, instead of scalar context,
you get the desired list directly, no need for split().


--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Sun, 20 Nov 2005 04:26:17 GMT  
 capturing output of executed system command

[ Please do not top-post ]

Quote:



>>> $ps=`ps`; # capture output of 'ps'

>> You're making this harder than it needs to be:


> thanks. didn't know you could do this.

Folks that do not read the documentation for the function that
they are using deserve any pain they get.

Try to avoid pain.

The description of backticks in perlop.pod describes both
scalar context and list context behaviors.

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Sun, 20 Nov 2005 07:53:39 GMT  
 capturing output of executed system command

Quote:



>>> system 'ps'

>>> how can i redirect the output into an array?

> [ snip sig, etc ]

>> $ps=`ps`; # capture output of 'ps'
>> print $ps ."\n"; # print output as a string.

> You're making this harder than it needs to be:



Don't forget


to get the effect of the "split /\n/, $ps" in the original code.
Otherwise you're leaving the trailing newlines.

Ted



Sun, 20 Nov 2005 15:54:13 GMT  
 capturing output of executed system command

Quote:

> suppose i have:

> system 'ps'

> how can i redirect the output into an array?

> thanks,
> benson

I prefer writing a "shell()" wrapper to do this sort of thing:

sub Shell {




Quote:
}

Then you can:


or run Shell() over a batch of commands rather easily:




Chris
-----
Chris Olive
Systems Consultant
Raytheon Technical Services Corporation
Indianapolis, IN

email: olivec(AT)indy(DOT)raytheon(DOT)com



Mon, 21 Nov 2005 18:31:31 GMT  
 capturing output of executed system command


Quote:

>> suppose i have:

>> system 'ps'

>> how can i redirect the output into an array?

>> thanks,
>> benson

>I prefer writing a "shell()" wrapper to do this sort of thing:

>sub Shell {




>}

>Then you can:



Seems like an incredibly complex bit of code to avoid:


or


Why exactly do you do it that way?



Tue, 22 Nov 2005 13:44:14 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Capturing System Command Output with Perl on Windows/Apache

2. Capturing output of a system command

3. Capture output from system command?

4. capturing system command output

5. capturing command line error or output from `<command>`

6. RE : DIFFICULT: Can't execute system command from FVWM, but can from the command line

7. How can I capture STDERR from a command executed externally

8. capturing output of FILEHANDLE "| command"

9. capturing output from a remote command (how?)

10. capturing output of piped commands

11. Most efficient method to call a system routine and capture it's output

12. Help with Piped command, capturing output

 

 
Powered by phpBB® Forum Software