mrlayance
mrlayance

Reputation: 663

Execute Perl script from within Perl script with variables

I believe I have this sort of correct. I can print the variables no problem. When passing the vriables to the ups check script nothing happeneds?

#!/usr/bin/perl
open FILE, "upslist.txt";

while ($line=<FILE>){
if ($line=~/^(.*?),(.*?)$/){
#print "ups:$1 string:$2\n";
do 'check_snmp_mgeups-0.1.pl -H $1 -C $2';
}        
}         

upslist.txt

#ups
ups1.site,upsstring1
ups2.site,upsstring1
ups3.site,upsstring2
ups4.site,upsstring3

Thanks for the help.

Upvotes: 0

Views: 104

Answers (3)

alex
alex

Reputation: 1304

You probably mixed quotes here. You need to use ` instead of ':

#!/usr/bin/perl
open FILE, "upslist.txt";

while ($line=<FILE>){
    if ($line=~/^(.*?),(.*?)$/){
        #print "ups:$1 string:$2\n";
        my $result = `check_snmp_mgeups-0.1.pl -H $1 -C $2`;
    }        
}         

Upvotes: 0

mob
mob

Reputation: 118665

You could do this with a local copy of @ARGV:

while ($line=<FILE>){
    if ($line=~/^(.*?),(.*?)$/){
        #print "ups:$1 string:$2\n";

        local @ARGV = ("-H", $1, "-C", $2);
        do 'check_snmp_mgeups-0.1.pl';
    }
}

but if you are seriously trying to integrate some functionality from the check_snmp_mgeups-0.1.pl script into your main script, consider a redesign for check_snmp_mgeups-0.1.pl as a reusable module that you can import into your script, and access its functionality through a subroutine call rather than the clunky do FILE mechanism.

Upvotes: 0

Joe Z
Joe Z

Reputation: 17956

You're using single-quotes here, which inhibits interpolation:

do 'check_snmp_mgeups-0.1.pl -H $1 -C $2';

Also, you probably wanted to use system here, not do.

system( "check_snmp_mgeups-0.1.pl", "-H", $1, "-C", $2 ) == 0 
   or die "system call to check_snmp_mgeups-0.1.pl failed: $?";

(Edited to use list form of system, and or instead of ||. My C++ was showing.)

Upvotes: 5

Related Questions