Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

execution of command inside for loop fails

by kaka_2 (Acolyte)
on Oct 01, 2013 at 13:23 UTC ( #1056478=perlquestion: print w/ replies, xml ) Need Help??
kaka_2 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks

i am writing a perl script to do small automation for my application. which require applcation's command to be run. first i use it in sub and it works well but next when i use inside the for loop it does not. can someone tell me what wrong i am doing.

sub ReadPolicies { # based on the below command it reads values in ar +ray my ($type) = @_; my @Policies = `/opt/OV/bin/opctemplate -l | grep -i $type | awk '{pri +nt \$2}'`; return (@Policies); } #MAIN My @FailoverPolicies = &ReadPolicies(DBSPI); For (my $i=0; $i < @FailoverPolicies; i++) { Print "/opt/OV/bin/opctemplate -e" . " " . $FailoverPolicies[$i], "\n" +; $output= `/opt/OV/bin/opctemplate e $FailoverPolicies[$i]`; # here i +expect this command read each value from the array and take action, - +e to enable }
-KAKA-

Comment on execution of command inside for loop fails
Download Code
Re: execution of command inside for loop fails
by jethro (Monsignor) on Oct 01, 2013 at 13:30 UTC

    I see "i++" instead of "$i++". I would suggest using foreach loop in such cases, no need for a loop variable.

    UPDATE: Please next time also add a description of how it is not working. You'll get much better answers that way

      Sorry. it was typo but i have $i++ in my script.

        That doesn't seem to be your only typo. Please use copy&paste to copy your actual code.

        In programming, every single character can be the difference between a program working and failing. It's not efficient at all to work with something that vaguely looks like your real code, but gets some details wrong.

Re: execution of command inside for loop fails
by hdb (Prior) on Oct 01, 2013 at 13:32 UTC

    If you add use strict; use warnings; at the beginning of your script, Perl will tell you what you are doing wrong.

    UPDATE: I cannot test this but I guess you need a chomp. Here is how I would write it:

    use strict; use warnings; sub ReadPolicies { my $type = shift; my @Policies = `/opt/OV/bin/opctemplate -l | grep -i $type | awk ' +{print \$2}'`; return @Policies; } for( ReadPolicies( "DBSPI" ) ){ chomp; # not sure this is required.... my $cmd = "/opt/OV/bin/opctemplate -e $_"; print "$cmd\n"; my $output= `$cmd`; print "$output\n"; }

      Thank You. adding strict and warnings was very helpful.

        Here is my updated code

        sub ReadPolicies { # based on the below command it reads values in ar +ray my ($type) = @_; my @Policies = `/opt/OV/bin/opctemplate -l | grep -i $type | awk '{pri +nt \$2}'`; return (@Policies); } #MAIN My @FailoverPolicies = &ReadPolicies(DBSPI); For (my $i=0; $i < @FailoverPolicies; $i++) { my $cmd "/opt/OV/bin/opctemplate -e" . " " . "$FailoverPolicies[$i]"; my $output= `$cmd`; # here i expect this command read each value from +the array and take action, -e to enable }

        and this works fine but the problem i face is that i see so many <defunct> process on system and i think it is because first command is not completed and second also start and so one and one point this hangs.

        What can i do to ensure 2nd command executes only when first is completed in the loop.

        Thank You.

        -KAKA-

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1056478]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2014-09-20 01:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (151 votes), past polls