LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 02:08 AM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

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, 05:19 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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 05:25 AM. Reason: Dimwit sometimes :$
 
Old 03-18-2010, 06:40 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

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

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, 12:33 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
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, 05:52 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
using awk in a perl script is like putting a diesel engine in a ferrari
 
Old 03-19-2010, 06:39 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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 06:43 AM.
 
  


Reply



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 07:09 PM
bash - difference between executing program in for loop and typing each command? jlarsen Programming 14 11-10-2008 05:38 PM
LXer: Using Bash To Feed Command Output To A While Loop Without Using Pipes! LXer Syndicated Linux News 0 08-06-2008 12:10 PM
Bash: command works but not inside of for loop. RijilV Programming 3 05-21-2006 08:29 PM
Loop Bash command to hit... rumperpumper Linux - General 4 01-31-2006 08:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:40 AM.

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
Open Source Consulting | Domain Registration