我有这段perl代码,我想让它做的是搜索字符串,如果找到匹配的字符串,就执行一些操作。
当它找到一个匹配项时,我希望它将计数器加1,这样它就不会再次遍历while循环。除了柜台之外,其他的都很好。不知何故,它一直在循环中。
我知道它是有效的,因为当我将计数器设置为1时,它不会进入循环。
#!/usr/bin/perl
use strict;
use warnings;
open (ERRORLOG, ">>/usr/local/bin/log.log") || die "failed to open errorlog file \n$!\n\a";
open (MESSAGES, "tail -1 /var/adm/messages |") || die "failed to open alarms file \n$!\n\a";
my $date=`date +"%d/%m/%y-%H:%M"`;
my $num_of_messages_sent;
while (my $line = <MESSAGES>) {
chomp($line);
if ($line =~ m/inetd|ypserv|cront|sys|kern|panic|scsi|ipmp|mpathd/i && $num_of_messages_sent < 1){
print ERRORLOG "$line\n";
print "$line\n";
print "$num_of_messages_sent\n";
do something
#$num_of_messages_sent = $num_of_messages_sent + 1;
$num_of_messages_sent++;
}
#$num_of_messages_sent = $num_of_messages_sent + 1;
#$num_of_messages_sent=0;
print "$num_of_messages_sent\n";
}
#$num_of_messages_sent = $num_of_messages_sent + 1;
$num_of_messages_sent=0;
close (ERRORLOG);
close (MESSAGES);
-----------------------second code----------------------------------------
#!/usr/bin/perl
use strict;
use warnings;
open (ERRORLOG, ">>/usr/local/bin/mcdl_errors.log") || die "failed to open errorlog file \n$!\n\a";
open (MESSAGES, "tail -1 /var/adm/messages | ") || die "failed to open alarms file \n$!\n\a";
## the last location checked is not at the end of messages file, we need to search the messages file
#count how many messages you need to send in 1 minute. we permit only 4 messages
#go over the messages file from the last location until the end
my $date=`date +"%d/%m/%y-%H:%M"`;
my $num_of_messages_sent=0;
my $line2compare = `more /usr/local/bin/line.txt`;
print "$line2compare\n";
while (my $line = <MESSAGES>) {
chomp($line);
if ($line =~ m/ypserv|cront|sys|kern|panic|scsi|ipmp|mpathd/i){
#print ERRORLOG "$line\n";
print "$line\n";
print "$num_of_messages_sent\n";
if ($line ne $line2compare) {
print ERRORLOG "$line\n";
print "$line2compare\n";
do something
$num_of_messages_sent++;
`echo $line > /usr/local/bin/line.txt`;
} else {
print "$line-Check this out\n";
}
print "$num_of_messages_sent\n";
last if $num_of_messages_sent>0;
}
}
close (ERRORLOG);
close (MESSAGES);发布于 2014-06-12 21:31:33
我认为您从来没有输入过if条件,因为您对文件执行了tail -1,这只给出了文件的最后一行。如果最后一行与模式不匹配,就不需要输入If条件,while就会立即结束,因为消息中只有一行。
你可以这样打开你的文件:
open (MESSAGES, "/var/adm/messages") || die "failed to open alarms file \n$!\n\a";删除if条件中的&& $num_of_messages_sent < 1,并在循环末尾添加一个last if $num_of_messages_sent>0;,如下所示:
while (my $line = <MESSAGES>) {
chomp($line);
if ($line =~ m/inetd|ypserv|cront|sys|kern|panic|scsi|ipmp|mpathd/i){
print ERRORLOG "$line\n";
print "$line\n";
print "$num_of_messages_sent\n";
do something
$num_of_messages_sent++;
}
print "$num_of_messages_sent\n";
last if $num_of_messages_sent>0;
}PS:我不知道它是否在您的正则表达式中,但是|字符被认为是OR。如果你想匹配|字符,你必须在它前面放一个\。
发布于 2014-06-13 03:33:12
两个问题:
首先使用tail -f而不是tail -1。在退出循环之前,您只读取了输出的一行。使用tail -f将跟随输出,您的程序将一直循环,直到您使用last语句。唯一的问题是,如果你不使用last,你的循环将永远不会退出。相反,当写入该日志的进程死亡时,您将永远坐在那里等待永远不会出现的队列。
我还通过在我的日志中查找某种类型的结束语句来绕过这个问题。例如,如果您正在使用Tomcat,您可能会看到有关应用程序停止的信息。这应该是你退出循环的提示。
另一个问题是你没有初始化你的计数器:
my $num_of_messages_sent;当您尝试递增该值时,会收到一条警告消息,并且您的程序可能会失败。而是:
my $num_of_messages_sent = 0;https://stackoverflow.com/questions/24180609
复制相似问题