restarting a while loop

I have a monitoring script that checks on the content of an alert file, I'm doing some checks on weather I have already reported on the alert (there is one alert per file). If I find that the content is the same as before how can I stop and restart the loop until there is differences bewteen the currect and previous log?

I was using a diff of two files and then trying to restart or continue depending on the status output (I had thought that break would do it but it exits from the loop).

Many fanks,

Neil

SunOS - ksh script :smiley:

Hi Neil,

Please give us some scripto, it's confusing to get things clear right now.

Please be aware of logsurfer the tool that analyzes (growing) logfiles online and actiones as given patterns are matched.

I have it in production on 5 HP11.00 servers right now, and it's maybe the best tool I ever seen.

Regs David

This is the bit that I suspect is failing, this fits into my while loop (the loop filters, extract records from log files and prints to screen.. etc)

If the last and current log is the same then I want it to go back to the beginning of loop (the extarct and checks will start all over again). If its not the same then I want it to conitune with the rest of the loop. Hope this helps and you can help???!!!!

echo $FIELDS > new_alert.tmp

diff new_alert.tmp last_alert.tmp
if [ $? -eq 0 ]
then
echo No new alert messsages `date`
sleep 15
???????;
else
continue;
fi

David,
Looked at the Logsurf webiste, i'm not too sure if these will work on XML type files, what sort of files do you use it on?

Cheers,
Neil

Since you didn't post your whole script (or at least the complete while loop ), the isn't much I can say about it since it works as far as checking for differences and sleeping. The minute it finds a difference, then it runs an endless loop - making assumtions about how you have it set up as follows:
last_alert.tmp is set up the same as new_alert.tmp (tail of /var/adm/messages).
while it's running, I change last_alert.tmp to cause the program to go to the else statement.

#!/bin/ksh
tail /var/adm/messages > new_alert.tmp
while true
do
diff new_alert.tmp last_alert.tmp
if [ $? -eq 0 ]
then
        echo No new alert messages `date`
        sleep 15
else
        continue
fi
done
exit

You really need to give more information.

Hmm is this file going to be running all the time, every 15 seconds? Maybe you should set up a cron job.

If not, then try this:

changed="NO"

while [ $changed == "NO" ]; then
do
  echo $FIELDS > new_alert.tmp
  diff new_alert.tmp last_alert.tmp
  if [ $? -eq 0 ]; then
    echo No new alert messsages `date`
    sleep 15
    changed="YES"
  else
    continue
  fi
done

---------
Oh well, now you have two ways. :slight_smile:

Hi there,

This is the complete loop, should have posted to begin with (doesn't give away any secrets). I did the 1 = 1 cos I just want it run for ever or until the user kills it. It checks if the error is new if not goes on to report on it on screen if it isn't I want it to recheck after a brief pause.

while [ 1 = 1 ]
do 

clear

integer EXT=1
ALERTDIR=/var/opt/projects/mnp/logs/alerts
CURRENTLOG=$ALERTDIR/alert.log.$EXT

# Moving on until a file is found
until [  -s $CURRENTLOG ] 
do
        EXT=`expr $EXT + 1`     
        CURRENTLOG=$ALERTDIR/alert.log.$EXT
done


# Makes the substitution of the fields in the alert logs.
FIELDS=`cat $CURRENTLOG | sed -e 's/">/, /g' \
             -e 's/<\/OBJECT>/, /g'  \
             -e 's/<//g'`

echo $FIELDS > new_alert.tmp

diff new_alert.tmp last_alert.tmp
        if [ $? -eq 0 ]
        then 
                echo No new alert messsages `date`
                sleep 15 
                ???????;
        else
                continue;
        fi

CODE=`echo $FIELDS | cut -f2 -d,`
DATE=`echo $FIELDS | cut -f4 -d,`


if [ $CODE = "T004" ]
then
        echo $FIELDS | cut -f2,4,12,15,17 -d, \
        \
        | awk -F"," '{print  ""; \
        print "ALERT ID   :" $1; \
        print "TIMESTAMP  :" $2; \
        print "ERROR CODE :" $3; \
        print "MSISDN     :" $4; \
        print "ERROR      :" $5}'

elif [ $CODE = "T002" ]
then
        echo $FIELDS | cut -f2,4,12,15,17 -d, \
        \
        | awk -F"," '{print ""; \
        print "ALERT ID   :" $1; \
        print "TIMESTAMP  :" $2; \
        print "ERROR CODE :" $3; \
        print "MSISDN     :" $4; \
        print "ERROR      :" $5}'

else 
        echo $FIELDS | cut -f2, -d, \
        \
        | awk -F"," '{print  ""; \
        print "ALERT ID   :" $1}' 
        echo "Unknown ERROR type, check with 3rd line"
fi

echo File `basename $CURRENTLOG`

cp new_alert.tmp last_alert.tmp
sleep 5
done

Many thanks is advance....

Neil

added code tags for readability --oombera