Shell scripting

March 15, 2009 at 03:09:15
Specs: Windows XP
Hi friends,
I am writing a Unix script to trigger mail. The condition is if there is a word called 'INVALID' in the input fiule it should triger a mail. But the file may have data for any number of days. But i should get mail only if 'INVALID' string is updated for current date.
ex: Input file is like this
FASTLOAD02 Mon Jun 16 07:50:11 2008 STARTING UP: Revision 1.16
FASTLOAD02 Mon Jun 16 07:50:11 2008 ALREADY RUNNING: ABORTING
FASTLOAD02 Mon Jun 16 07:50:11 2008 CLOSING DOWN: |
FASTLOAD02 Tue Jun 17 08:12:48 2008 STARTING UP: Revision 1.16
FASTLOAD02 Tue Jun 17 08:12:48 2008 ALREADY RUNNING: ABORTING
FASTLOAD02 Tue Jun 17 08:12:48 2008 CLOSING DOWN: |
FASTLOAD02 Thu Mar 12 12:19:06 2009 INVALID NUMBER OF FIELDS: |

If Input file is like this. It should not trigger mail.But if input file is like the below
FASTLOAD02 Tue Jun 17 08:12:48 2008 STARTING UP: Revision 1.16
FASTLOAD02 Tue Jun 17 08:12:48 2008 ALREADY RUNNING: ABORTING
FASTLOAD02 Tue Jun 17 08:12:48 2008 CLOSING DOWN: |
FASTLOAD02 Thu Mar 12 12:19:06 2009 INVALID NUMBER OF FIELDS: |
FASTLOAD02 Thu Mar 15 12:19:06 2009 INVALID NUMBER OF FIELDS: |


With 'INVALID' string for current date it should trigger a mail. I have written a script for checking invalid. BUt facing difficulties when including date logic. Kindly help
. This is my current script without date logic
#!/bin/ksh
. $HOME/.profile
#set -xv
MAIL=Mail_id
File=Input file path
LOG_FILE=Log file path
#Checking for the INVALID string in FASTLOAD.ssrdep File#
grep -w "INVALID" $File

if [ $?=0 ]
then
echo `grep -l "INVALID" $File` > $LOG_FILE
echo "Found 'INVALID'string in FASTLOAD logs.Please check $LOG_FILE" |mailx -s "Found 'INVALID' string in FASTLOAD lo
gs" $MAIL
fi
Kindly help. Thanks pals.


See More: Shell scripting

Report •


#1
March 15, 2009 at 14:02:25
A way is to parse the file and choose only lines where the 3rd and 4th fields equals today's month and day respectively. Then, only grep the INVALID string:

#!/bin/ksh

# set todays month and day to the first two arguments
set - $(date +'%b %e')
mm=$1
dd=$2

while read col1 col2 mymm mydd therest
do
    if [[ $mm == $mymm && $dd == $mydd ]]
    then
       if echo "$therest"| grep INVALID > /dev/null
       then
           echo "Found INVALID in FASTLOAD ...."
      fi
   fi
done < datafile


Report •

#2
March 16, 2009 at 04:42:51

Hi nails,
The file which i consider has lakh bytrs of data for today's date itself. So segregating using INVALID string with grep option might filter maximum data. SO i tried this way
#!/bin/ksh
set -xv
MAIL="mail id"

INPUT_FILE=/Input dir
LOG_FILE=fastload.log
#Getting Month and Day from system time
CUR_DATE=$(date "+'%b %d')
echo "Hi Date is " $CUR_DATE
#CUR_DATE="$CURRENT_DATE"
#Checking for the INVALID string for current date in FASTLOAD*.ssrdep File
grep -w 'INVALID' $INPUT_FILE | grep "$CUR_DATE"
#grep $CUR_DATE |grep -w 'INVALID' $INPUT_FILE
if [$?=0]
then
#echo $DATE_CHECK
echo $CUR_DATE
#if [ "$DATE_CHECK"="$CUR_DATE" ];
#then
echo `grep -l "INVALID" $INPUT_FILE` > $LOG_FILE
echo "Found 'INVALID'string in FASTLOAD logs.Please check $LOG_FILE" |mailx -s "Found 'INVALID' string in FASTLOAD lo
gs" $MAIL
else echo "INVALID STRING IS NOT FOUND" > LOG_FILE
#echo "INVALID STRING IS NOT FOUND" > LOG_FILE
fi


The above code does not seems to work. Its producing error at grep field (grep $CUR_DATE). If i hard code the exact date it works but, if i assign a date in variable CUR_DATE and then use grep $CUR_DATE. Its not working.Kindly help


Report •

Related Solutions


Ask Question