Monitoring script issue

I written following script to monitor two log files. First piece of code working fine, but piece is failing with followin error.

$ sh Log_Monitoring.sh
Database failed to written
4
Database failed to written
expr: syntax error

=================

clear
sh=`date | awk '{print $4}' | cut -d ":" -f1`
sm=`date | awk '{print $4}' | cut -d ":" -f2`
lh=`ls -l /opt/Heartbeat.log | awk '{print $8}' | cut -d ":" -f1`
lm=`ls -l /opt/Heartbeat.log | awk '{print $8}' | cut -d ":" -f2`
if [ x = 0 ]
then
if [ -f "/export/home/paven/interval_core.dat" ]; then
echo "0" > /export/home/paven/interval_core.dat
fi
else
x=`cat /export/home/paven/interval_core.dat`
if [ "$sh" = "$lh" ]
then
if [ "$sm" = "$lm" ]
then
echo "Database writting properly"
echo "0" > /export/home/paven/interval_core.dat
else
echo "Database failed to written"
x=`expr $x + 1`
echo "$x" > /export/home/paven/interval_core.dat
echo $x
if [ "$x" = "2" ]
then
#mailx -s "Data base failed to write in Core Environment - Need to retrieve missing data" -c "xyz@abc.com"<<EOF
echo Data base failed to write in Core Environment - Need to retrieve missing data
fi
fi
fi
fi

sleep 10

sh1=`date | awk '{print $4}' | cut -d ":" -f1`
sm1=`date | awk '{print $4}' | cut -d ":" -f2`
lh1=`ls -l /opt/NH_Heartbeat.log | awk '{print $8}' | cut -d ":" -f1`
lm1=`ls -l /opt/NH_Heartbeat.log | awk '{print $8}' | cut -d ":" -f2`
if [ y = 0 ]
then
if [ -f "/export/home/paven/interval_NH.dat" ]; then
echo "0" > /export/home/paven/interval_NH.dat
fi
else
y=`cat /export/home/paven/interval_NH.dat`
if [ "$sh1" = "$lh1" ]
then
if [ "$sm1" = "$lm1" ]
then
echo "Database writting properly"
echo "0" > /export/home/paven/interval_NH.dat
else
echo "Database failed to written"
y=`expr $y + 1`
echo "$y" > /export/home/paven/interval_NH.dat
echo $y
if [ "$y" = "2" ]
then
#mailx -s "Data base failed to write in NHS Environment - Need to retrieve missing data" -c "xyz@abc.com"<<EOF
echo Data base failed to write - Need to retrieve missing data
fi
fi
fi
fi
================:)

The syntax error is becuse there is nothing in $x or $y at the time the respective expr statements are issued.

Without trying to understand the logic of your script, the dubious lines are:

You may find that this helps:

# Near top of script to fix the later "expr" lines.
x=0
y=0
# Numeric comparisions with $x and $y.
if [ $x -eq 0 ]

if [ $y -eq 0 ]

Variables passed to expr (x or y) are not initialized.
You are assigning values to x or y in if/else block, so depending on conditions, x/y may not be initialized.
Pls make sure you initilize x and y out of if/else bolck.
Have a look at your earlier post:

1 Like