Help needed with script.

Hi,

Basically I am looking to search for a certain string within a log file. If this string is present then I want to send an output file with the results of this string in it to me by email. If the output file has 0 as it's result the we email a successful backup. Script is below.

#!/bin/bash
LOG_FILE=/var/adm/messages
export LOG_FILE
OUTPUT_FILE=/tmp/logerrors
export OUTPUT_FILE
cat $LOG_FILE|grep -i error | wc -l >> $OUTPUT_FILE
if [ test $OUTPUT_FILE -gt 0 ];
then
        echo "Subject: BACKUP FAILED" >> $OUTPUT_FILE
   mailx  -s "BACKUP FAILED" johndoe@test.com < $OUTPUT_FILE
else
  mailx  -s "Sucessful RMAN backup" johndoe@test.com
fi

Thanks for your help.:o

Sparcman

1) What do you mean by "$OUTPUT_FILE -gt 0" ?
In this way, you are just comparing the value of the variable ( i.e the path of the filename) with zero. which is wrong.
I guess, you want to compare the size of the file.

2) ">>" means you are appending to the file instead creating new file. if there is already existing file with that name, your count may go wrong.

3) [ ] itself used as a test operator. so no need of explicitly use of mentioning test keyword.

4) There is one useless use of cat too.

Try something like:

LOG_FILE=/var/adm/messages
export LOG_FILE
OUTPUT_FILE=/tmp/logerrors
export OUTPUT_FILE
grep -i error $LOG_FILE| wc -l > $OUTPUT_FILE
if [ -s "$OUTPUT_FILE" ];
then
        echo "Subject: BACKUP FAILED" >> $OUTPUT_FILE
        mailx  -s "BACKUP FAILED" johndoe@test.com < $OUTPUT_FILE
else
        mailx  -s "Sucessful RMAN backup" johndoe@test.com
fi

1 Like

Thanks Anchal Khare.

That script is now almost doing what I need. Sorry - I should have explained it better. If the value in the output file is 0 then the backup will have been successful as there are no errors. Any value greater than that signifies an error has occured and I need to investigate.

I have recieved a mail which had a value of 0 in it and "Backup Failed" as the subject.

Basically what is to test the value in the output file and if it is equal to 0 then the backup was successful. Anything more than 0 and the backup failed.

grep -i error $LOG_FILE| wc -l > $OUTPUT_FILE
-bash-3.00$ cat $OUTPUT_FILE
0
Subject: BACKUP FAILED ----> This needs to be a successful backup
as there are no errors. Anything
greater than 0 is a backup failure.

Thanks for looking into this for me.

Sparcman.:b:

another thing you can do is to make the mail more useful is use a "here" document and
insert things using $(command) or `command` like so:

mailx  -s "BACKUP FAILED" johndoe@test.com <<EOF

we got blah blah blah
================
$(date)
`hostname`
disk usage:
`df`

output file is:
$(cat $OUTFILE)


EOF
1 Like

Ok, If that is the case, then

if [ "$(cat $OUTPUT_FILE)" -eq "0" ]; then
 echo 'success'
else
 echo 'failed'
fi
LOG_FILE=/var/adm/messages
export LOG_FILE
OUTPUT_FILE=/tmp/logerrors
export OUTPUT_FILE
grep -i error $LOG_FILE| wc -l > $OUTPUT_FILE
if [ "$(cat $OUTPUT_FILE)" -eq "0" ];then
        mailx  -s "Sucessful RMAN backup" johndoe@test.com
else
        echo "Subject: BACKUP FAILED" >> $OUTPUT_FILE
        mailx  -s "BACKUP FAILED" johndoe@test.com < $OUTPUT_FILE
fi

even you don't need the output file.

COUNT=$(grep -i error $LOG_FILE| wc -l )

Now, Do all the comparison based on this variable.

1 Like

Thanks Anchal Khare. Thats working now! Your help was much appreciated.

Sparcman:b: