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. |
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}'`; |
|
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 |
Quote:
|
using awk in a perl script is like putting a diesel engine in a ferrari
|
you can print arrays:
Code:
@array = qx/ps auxww/; 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); |
All times are GMT -5. The time now is 04:13 AM. |