June 19, 2011

Script to monitor files (with a file name prefix) are updating in a directory

#!/bin/sh
OPCMON="/opt/OV/bin/opcmon"
MON_NAME=$1

line1=`eval date +'%Y%m%d/MSC'`

##### Count_M10C1.txt file keeps the count of total number of files in the input directory #########

FILE_M10C1="/tmp/count_M10C1.txt"
if [ ! -f $FILE_M10C1 ]; then
echo "File Does Not Exist"
`ls $line1 | grep M10C1 | wc -l >> /tmp/count_M10C1.txt`
exit 1
fi

var=`cat $FILE_M10C1`
for i in $var;
do
line=$i
done


##### Filecount is the variable which stores the current number of files in input directory ##########

Filecount=`ls "$line1" | grep M10C1* | wc -l`

##### Comparison is being done whether the current number of files in input directory is equal to the previous count #######

if [ $Filecount = $line ];
then
echo "Count Equal"
/opt/OV/bin/opcmon "${MON_NAME}=1"
else
echo "Count Unequal"
/opt/OV/bin/opcmon "${MON_NAME}=0"
fi


###### Store the current number of files in input directory to temporary file count_M10C1.txt ########
`ls $line1 | grep M10C1* | wc -l > /tmp/count_M10C1.txt`