LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-18-2010, 03:08 AM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 348

Rep: Reputation: 16
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.
 
Old 03-18-2010, 06:19 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
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

Last edited by grail; 03-18-2010 at 06:25 AM. Reason: Dimwit sometimes :$
 
Old 03-18-2010, 07:40 AM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Or use qx http://perldoc.perl.org/perlop.html#...Like-Operators
 
Old 03-18-2010, 01:17 PM   #4
casperdaghost
Member
 
Registered: Aug 2009
Posts: 348

Original Poster
Rep: Reputation: 16
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
 
Old 03-18-2010, 01:33 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by casperdaghost View Post
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'.
 
Old 03-19-2010, 06:52 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
using awk in a perl script is like putting a diesel engine in a ferrari
 
Old 03-19-2010, 07:39 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
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!

Last edited by bigearsbilly; 03-19-2010 at 07:43 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop through all files in a directory. Bash/Perl script? Nzo Linux - Newbie 9 12-09-2009 08:09 PM
bash - difference between executing program in for loop and typing each command? jlarsen Programming 14 11-10-2008 06:38 PM
LXer: Using Bash To Feed Command Output To A While Loop Without Using Pipes! LXer Syndicated Linux News 0 08-06-2008 01:10 PM
Bash: command works but not inside of for loop. RijilV Programming 3 05-21-2006 09:29 PM
Loop Bash command to hit... rumperpumper Linux - General 4 01-31-2006 09:39 PM


All times are GMT -5. The time now is 09:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration