reading all process numbers into an array

D

dn_perl

From command line, the following command gives IDs of all the
processes.
ps -ef | awk -F" " '{print $2}'

But if I use the command in a perl script within backticks, I get full
listing in the array instead of just the process IDs.

my @list_of_procs ;
@list_of_procs = ` ps -ef | awk -F" " '{print $2}' ` ;

Why is this so and how do I create an array with just the process IDs?


Thanks in advance.
 
B

Ben Morrow

Quoth (e-mail address removed) ([email protected]):
From command line, the following command gives IDs of all the
processes.
ps -ef | awk -F" " '{print $2}'

But if I use the command in a perl script within backticks, I get full
listing in the array instead of just the process IDs.

my @list_of_procs ;
@list_of_procs = ` ps -ef | awk -F" " '{print $2}' ` ;

Why is this so and how do I create an array with just the process IDs?

Use Proc::processTable instead, or at least do the split in perl:

my @list_of_procs = map { (split)[1] } `ps -ef`;

The reason the awk fails is because `` do double-quotey interpolation,
so you need

@list_of_procs = `ps -ef | awk -F" " '{print \$2}'`;

or

my $cmd = q/ps -ef | awk -F" " '{print $2}'/;
my @list_of_procs = qx/$cmd/;

Ben
 
G

Gregory Toomey

From command line, the following command gives IDs of all the
processes.
ps -ef | awk -F" " '{print $2}'

But if I use the command in a perl script within backticks, I get full
listing in the array instead of just the process IDs.

my @list_of_procs ;
@list_of_procs = ` ps -ef | awk -F" " '{print $2}' ` ;

Why is this so and how do I create an array with just the process IDs?


Thanks in advance.

You probably want (under linux)

for (split '\n', qx(ps -o pid --no-headers)) {
...
}

gtoomey
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,154
Messages
2,570,870
Members
47,400
Latest member
FloridaFvt

Latest Threads

Top