I am writing this simple code to get alert email when "error" found on my log.
The question is I want to get only one alert per scan my code is generate number of alert based number of error matched on the file.
open (LOG, "<$log") || die 'Could not OPEN log file';
while ($loglines = <LOG> ) {
if ($loglines =~ /Error/) {
print "Error on the log \n";
}
}
close (LOG);
the result looks like
Error on the log
Error on the log
Error on the log
Error on the log
Error on the log
Instead I want just one print "Error on the log" that i way I can get one email when error matched on the log file. What did I miss?
There are a lot of improvements in that. The most important change is the addition of
last;; it terminates the loop on the first match.Other changes are:
or‘ instead of ‘||‘.$loglines(assuming you didn’t need/use it elsewhere).Some would prefer a label added to the loop and then an explicit use of that in the
laststatement:I’m not convinced there is much gain here, assuming there are no enclosing loops. If there are any enclosing loops, then use the label.
I assume you are using:
at the top of your script? If not, then do so. Perl experts use them to make sure they avoid making silly mistakes; Perl novices should use them for the same reasons.