【发布时间】:2014-12-04 12:16:26
【问题描述】:
编辑:重写示例以使其更清晰
我将数据放入一个名为 RTR-shpolicymap_751257S0_126.xxx.xxx.xxx_OUTPUT 的文件中:
751257S0;126.xxx;FastEthernet0;up;up;not set;not set;;
751257S0;126.xxx;BRI0;down;down;not set;not set;;
751257S0;126.xxx;BRI0:1;down;down;not set;not set;;
751257S0;126.xxx;BRI0:2;down;down;not set;not set;;
751257S0;126.xxx;ATM0;up;up;not set;not set;;
751257S0;126.xxx;ATM0.835;up;up;not set;not set;;
一个perl脚本执行一个到路由器的SSH连接,当连接建立时,它调用函数executeCommands 这里描述了这个功能:
sub executeCommands {
open(SSHCONFIG, "/tech/restools/scripts/mynet/RTR-ssh.conf");
while (<SSHCONFIG>) {
@ligne = split(/;/, $_ );
$listop = $ligne[0];
$listcmd = $ligne[1];
$fileprefix = $ligne[2];
$parsername = $ligne[3];
$parsername =~ s/\s+\z// ;
@cmd = split(/,/, $listcmd);
#Si l'operateur passe en parametre match l'operateur dans le fichier de config - execution des commandes
if ($listop =~ /$operateur/) {
for ($index = 0; $index <= $#cmd; $index++) {
$command = $cmd[$index];
&changeCommand(\$command);
&sendCommand($command, $fileprefix);
}
}
else {
next;
}
配置文件包含以下内容:
OBS,9 Cegetel,Altitude;sh policy-map $policyinterface;RTR-shpolicymap;RTR-parser9.pl
现在,我遇到了函数 changeCommand
的问题sub changeCommand {
my $arg1 = $_[0];
if ($$arg1=~/^(.*)(\$policyinterface)(.*)/) {
print "recherche de l'interface contenant la policy voice correspondante... \n" ;
$fichier_parser1OUTPUT = `find $WORKINGDIR -type f -name RTR-shipint_\"*$codesite*$ip*OUTPUT\"` ;
@table_fichier_parser1OUTPUT = split(/\n/,$fichier_parser1OUTPUT);
for ($index4 = 0 ; $index4 <= $#table_fichier_parser1OUTPUT ; $index4++) {
$fichierNeeded = $table_fichier_parser1OUTPUT[$index4];
open(DATA,$fichierNeeded) || die ("Erreur d'ouverture de $fichierNeeded\n");
print "file $fichierNeeded have been opened \n";
while (my $lineData=<DATA>) {
print "i am printing this line $lineData \n";
my @lineSplitter = split(/;/, $lineData );
print "3e arg est <<$lineSplitter[2]>>\n";
}
$command = "sh policy-map int " . $interfaceNeeded ;
}
}
}
问题就在那里,这就是我执行脚本时发生的情况:
recherche de l'interface contenant la policy voice correspondante...
file /tech/restools/tmp/mynet/RTR-working-dir/RTR-shipint_751257S0_126.108.2.250_OUTPUT have been opened
i am printing this line 751257S0;126.108.2.250;FastEthernet0;up;up;not set;not set;;
3e arg est <<FastEthernet0>>
我的错误在哪里?是因为我已经在之前的函数中使用了 $_ 吗?
单独测试一个类似的脚本正在工作......
#!/usr/bin/perl
use strict ;
my $codesite="751257S0" ;
my $ip = "126.xxx.xxx.xxx" ;
my $WORKINGDIR="/tech/restools/tmp/mynet/RTR-working-dir";
my $fichier_parser1OUTPUT = `find $WORKINGDIR -type f -name RTR-shipint_\"*$codesite*$ip*OUTPUT\"` ;
my @table_fichier_parser1OUTPUT = split(/\n/,$fichier_parser1OUTPUT);
for (my $index4 = 0 ; $index4 <= $#table_fichier_parser1OUTPUT ; $index4++) {
my $fichierNeeded = $table_fichier_parser1OUTPUT[$index4];
open(DATA,$fichierNeeded) || die ("Erreur d'ouverture de $fichierNeeded\n");
print "file $fichierNeeded have been opened \n";
while (my $lineData=<DATA>) {
print "i am printing this line $lineData \n";
my @lineSplitter = split(/;/, $lineData );
print "3e arg est <<$lineSplitter[2]>>\n";
}
}
打印
i am printing this line 751257S0;126.108.2.250;FastEthernet0;up;up;not set;not set;;
3e arg est <<FastEthernet0>>
i am printing this line 751257S0;126.108.2.250;BRI0;down;down;not set;not set;;
3e arg est <<BRI0>>
i am printing this line 751257S0;126.108.2.250;BRI0:1;down;down;not set;not set;;
3e arg est <<BRI0:1>>
...
【问题讨论】:
-
是的,添加打印标量显示“1”
-
文件是否有 Windows 行结尾?
-
split仍然会产生几行,因为 Windows 在其行尾有\n。 -
请阅读如何创建 MCVE (Minimal, Complete, Verifiable Example) 或 SSCCE (Short, Self-Contained, Correct Example) — 两个名称和链接用于相同的基本思想。前三行与您的问题无关;您需要设置文件名,然后打开并使用它。您是否在
split之前打印了$_?您是否在脚本的前面将$/设置为奇数,或者它是否未定义(通过local $/;)以便整个文件被吞食?您是否打印了所有拆分字段?这些是基本的调试技术。 -
@AKHolland:Windows 行尾仍包含换行符,因此行被正确分隔。问题来自
chomp,它删除了尾随的换行符,但保留了 CR。