簡體   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