LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 11-29-2007, 08:25 PM   #1
new_2_unix
LQ Newbie
 
Registered: Oct 2007
Posts: 26

Rep: Reputation: 15
Perl - how to capture output of a command with more than one args


hi,

i'm writing a perl script where i need to run a user defined command (a shell script) and capture the output.

i can do this as follows and it works fine:

Code:
my @list = `mycommand arg1 arg2`;
however, in the above example, i hard-coded the arg1, arg2 inside the backticks. if i want to declare a scalar variable, which basically holds the value as:

Code:
my $x = "arg1";
my $y = "arg2";

my @list = `mycommand $x $y`; //does not work 
my @list = `mycommand $x`; // works
Am I executing the line that does not work above in a wrong way? When I say 'does not work' I mean that when I iterate over my list, it doesn't have anything in it.

Any guidance will be helpful. thanks.

Last edited by new_2_unix; 11-29-2007 at 08:59 PM.
 
Old 11-29-2007, 10:19 PM   #2
Slokunshialgo
Member
 
Registered: Jan 2006
Distribution: Ubuntu 9.04, Fedora 10
Posts: 76

Rep: Reputation: 17
Actually looked this up a couple minutes ago for myself...

You can use the ARGV array to get your variables, and add them all either into a string, or an array. below is the method for a single string...

For example...

Code:
$args = "";
foreach (@ARGV) {
  $args = $args," ",$_;
}
`command $args`;
Where I looked this up from a bit before reading this... http://www.devdaily.com/perl/edu/qanda/plqa00001.shtml
 
Old 11-29-2007, 10:55 PM   #3
new_2_unix
LQ Newbie
 
Registered: Oct 2007
Posts: 26

Original Poster
Rep: Reputation: 15
That works!!
Thanks!
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Capture partial output from `date` command tekmann33 Linux - Newbie 7 12-27-2006 01:09 PM
can't capture 'at' command standard output to a file bartoni Linux - General 3 02-28-2006 03:52 PM
processing command args in bash slzckboy Programming 10 09-28-2005 04:49 PM
perl: passing 2 individual arrays/lists as args johnMG Programming 12 04-18-2005 03:49 PM
>>> control mplayer with command args??? nibjb Linux - Software 0 07-16-2003 11:09 PM


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

Main Menu
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