CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Executing command within perl

 



frankigeno
New User

Jul 31, 2012, 11:04 AM

Post #1 of 4 (908 views)
Executing command within perl Can't Post

Hi,

I need to execute a unix command within perl and I am using:

$result = `/root/mycommand.sh`;

The result of mycommand.sh is a lot of lines which I need to parse individually.

What would be the best way to store this result and send it to a loop to parse each line individually.

I have tried;

while (result) {
print $_;
$raw=$raw+1;
}

Which works fine but it hangs there since it doesnt recognize that It's reached the last line.



Many thanks for your help,
Francesco


Laurent_R
Veteran / Moderator

Jul 31, 2012, 11:42 AM

Post #2 of 4 (906 views)
Re: [frankigeno] Executing command within perl [In reply to] Can't Post

If you need to read the lines one by one, you should probably assign the result to an array, rather than a scalar.

Something like this:


Code
my @result = `/root/mycommand.sh`;


You can then work on each element of the array:


Code
foreach my $line (@result) { 
chomp $line;
# do something on your lines
}



frankigeno
New User

Jul 31, 2012, 11:48 AM

Post #3 of 4 (904 views)
Re: [Laurent_R] Executing command within perl [In reply to] Can't Post

Many thanks for it,

In fact I had also found a solution on this website:
http://www.perlmonks.org/?node_id=745018

which is very similar to yours.

I will try this and let you know,
Thanks,
Francesco


frankigeno
New User

Jul 31, 2012, 11:52 AM

Post #4 of 4 (902 views)
Re: [frankigeno] Executing command within perl [In reply to] Can't Post

Yes,

it works.
Thank you again,
Francesco

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives