On 25 Jun 2001 14:13:22 -0500, Tom Yarrish wrote:
> Hey all,
> Okay, I'm still working on my little Net::Telnet project, but a different
> section of the code is giving me a problem.  I thought I had an
> understanding of regex an if/else, but apparently not.
> Here's the code snippet:
> 
> 
> 
> open(TAIL, "tail $glmiss_log|") or die "Can't tail on $glmiss_log: $!\n";
> 
> while (my $status = <TAIL>){
>         if ($status =~ /SERVICE NOT AVAILABLE/){
>                 print "Service not running, attempting to start.\n";
>                 print "This may take a few minutes.\n";
>                 $fix = 1;
>         } else {
>                 print "Service is connected.\n";
>                 $fix = 0;
>                 exit;
>         }
> }
> 
> Okay, the $glmiss_log points to a logfile, and for test purposes I'm using
> one that has the "Service not available" error at the end of it.  So the
> first part of the if statement should pick it up.  Here's what the error
> looks like in the log:
> 
> WARNG 05:02:10 VCI_Login :   TECH_VCI_ERROR 00120 - EXCHANGE SERVICE NOT
> AVAILABLE
> 
> Now if I put [] around the regex, it matches it, but without it skips and
> goes right to the else statement.  Do I need to put any pattern matching
> for the rest of the line?  I basically just need it to see this error
> (which if there's a problem will be the last line in the log file) and
> then I have it do something once it exits the loop.
> 
> Thanks ahead of time,
> Tom
<snip />

Question:  the line starting with WARNG comes out as two lines in my
email client.  Is this one line or two.  If it is really two lines then
the problem is that the string you are looking for is really "SERVICE
NOT\nAVAILABLE".  

Brackets create a character class, so if you write

if ($status =~ /[SERVICE NOT AVAILABLE]/){

you are really matching against any string that has any of the
characters S, E, R, V, I, C, " ", N, O, T, A, L, or B in it.  Most
likely you want this regexp:

if ($status =~ /SERVICE\sNOT\sAVAILABLE/){

\s is a character class the contains all whitespace characters (ie tab,
LF, CR, space, etc)
 
--
Today is Sweetmorn, the 30th day of Confusion in the YOLD 3167
This statement is false.


Reply via email to