繁体   English   中英

grep命令来计算perl中的警报

[英]grep command to count the alerts in perl

我有以下脚本:

    # Find any connection alerts to report
    for my $Alert (@Alerts)
    {   
        print " $Alert->{type} alerts:\n";

        print STDERR "Query: [\n$Alert->{query}]\n";
        my $alertCur = $dbh->prepare($Alert->{query})
          or die "Unable to prepare cursor for [$Alert->{query}]: $DBI::errstr\n";      

        $alertCur->execute() 
          or die " Unable to execute cursor: $DBI::errstr\n";

        while (my $rec = $alertCur->fetchrow_hashref())
        {               
            #already in alert, don't send
            next if (defined $rec->{alert_id});     

            print $fh "Location Alert Notification: $Alert->{type} for Site $rec->{location_id}  elapsedTime(D HH:MM):  $rec->{diff_tm}\n";
            print "Location Alert Notification: $Alert->{type} for Site $rec->{location_id}  elapsedTime(D HH:MM):  $rec->{diff_tm}\n";
            $dbh->do ("($Alert->{alert_id}, $rec->{location_id}, $rec->{imperial_site_id}, current)") or die "Unable to insert alert: $DBI::errstr\n";                                                          
            $alertCnt++;                                                                                                    
        }
    }

    $dbh->disconnect();

    if ($alertCnt > 0)
    {
        print "Found <$alertCnt> alerts!\n";
    }
    else
    {
        print "No alerts found!\n"
    }

    print "Finished processing, elapsed seconds <" . (time() - $startTime) . ">\n";

    exit(0);

我试过了:

    ps -ef|grep myscript.pl|grep $alertCnt grep

返回:

    52642 117527  0 11:15 pts/63   00:00:00 grep myscript.pl

$alertCnt是存储已发生警报数量的变量。我想计算变量$alertCnts给定的警报数量。在这里获取警报计数数量的命令的正确用法是什么?

请记住,Perl具有非常灵活的内置grep函数。 您可以在大括号之间嵌入所需的任何perl语句,如下所示:

#!/usr/bin/env perl

grep { 
# do something with regex or whatever.
} `ps -ef` # can be any old command-line invocation

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM