【问题标题】:Execute Perl script from within Perl script with variables使用变量从 Perl 脚本中执行 Perl 脚本
【发布时间】:2013-12-11 03:34:26
【问题描述】:

我相信我有这种正确的。我可以打印变量没问题。将变量传递给 ups 检查脚本时没有任何反应?

#!/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

感谢您的帮助。

【问题讨论】:

  • 你能发几行upslist.txt吗?

标签: perl


【解决方案1】:

您在这里使用单引号,这会抑制插值:

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

另外,您可能想在这里使用system,而不是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: $?";

(编辑为使用systemor 的列表形式,而不是||。我的C++ 正在显示。)

【讨论】:

  • 不确定我在这里做错了什么。我得到:对 check_snmp_mgeups-0.1.pl 的系统调用失败:-1 at ups_crunch 第 11 行, 第 2 行。
  • 您可能需要将check_snmp_mgeups-0.1.pl 的完整路径放在那里。此外,如果check_snmp_mgeups-0.1pl 脚本未标记为可执行,那么您可能需要chmod +x 它。
【解决方案2】:

您可以使用local@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';
    }
}

但是,如果您正在认真尝试将 check_snmp_mgeups-0.1.pl 脚本中的某些功能集成到您的主脚本中,请考虑将 check_snmp_mgeups-0.1.pl 重新设计为可重用的模块,您可以将其导入到您的脚本中,并通过子例程访问其功能调用而不是笨拙的do FILE 机制。

【讨论】:

    【解决方案3】:

    您可能在这里混用了引号。您需要使用 ` 而不是 ':

    #!/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`;
        }        
    }         
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多