LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash for loop in perl - command interpolation (https://www.linuxquestions.org/questions/programming-9/bash-for-loop-in-perl-command-interpolation-796213/)

casperdaghost 03-18-2010 02:08 AM

bash for loop in perl - command interpolation
 
bash can interpolate a command to create a list.

#!/usr/bin/sh
for $i in $(ps auxwww | awk '{print $2}') ;
do ;
echo $i ;
done ;

the output of this command is a nice, neat column of PID's

I trying to do the same thing in Perl - and load the PID's into an array.
But do not seem to be able to load the results of the ps command into an array.

how do i interpolate a command in perl

#!/usr/bin/perl -wT
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
print header;
print start_html("this is the title");


my @pidnumbers = qw( $(ps auxwww | awk '{print $2}' ) );
my $size = @pidnumbers;
for (my $i = 0; $i <= $size; $i++) {
print "$pidnumbers[$i]\n"; }

this output of this script is $(ps auxwww | awk '{print $2}' ) on the browser.

grail 03-18-2010 05:19 AM

Hey casperdaghost

It seems you may be confusing bach ettiquette and perl ( I could be wrong ), but
perl still only uses the backtick (`) and not the bash option of $() to run commands.
Also, your output is correct as qw() is doing its job of putting all the words you put in there
into your array variable pidnumbers. I would be guessing your $size = 1.

So try

Code:

my @pidnumbers = `ps auxwww | awk '{print \$2}'`;
Edit: Wouldn't you know it <doh> forgot the escape character. This should work now

chrism01 03-18-2010 06:40 AM

Or use qx http://perldoc.perl.org/perlop.html#...Like-Operators

casperdaghost 03-18-2010 12:17 PM

qx and backticks work -
 
i am making the transition from bash to perl in my day to day assignments. i had to take off the perl -wT flag to get it to work. both the qx and backticks did the job - both operations needed the awk variable \escaped. there is probably a better perl way of doing this.
however the qx and quotelike operator list should help ease the transition

thanks

Sergei Steshenko 03-18-2010 12:33 PM

Quote:

Originally Posted by casperdaghost (Post 3903428)
i am making the transition from bash to perl in my day to day assignments. i had to take off the perl -wT flag to get it to work. both the qx and backticks did the job - both operations needed the awk variable \escaped. there is probably a better perl way of doing this.
however the qx and quotelike operator list should help ease the transition

thanks

And with Perl you do not need 'awk' - in your case 'split' *built-in function) will do the job of 'awk'.

bigearsbilly 03-19-2010 05:52 AM

using awk in a perl script is like putting a diesel engine in a ferrari

bigearsbilly 03-19-2010 06:39 AM

you can print arrays:
Code:

@array = qx/ps auxww/;

print "@array";
print "pid:$_\n" foreach (@array);
foreach my $thing (@array) {
  print "pid:$thing\n";
}

I see you are using CGI.
if, for instance you want to print the pids in a table <tr>pid</tr> or maybe
paragraphs <p>pid</p>

you do it like so:

Code:

print Tr(\@pids);
print p(\@pids);

simples!


All times are GMT -5. The time now is 04:13 AM.