Actually I am trying to make a script that will scan for the errors in the log file. It will compare the previous count of the errors found in the last count with the current count of errors in the present scan. If the current count is more than the previous count , then it will echo "1".
Now this script will be run again after few minutes again.
At this time we need to take count in the fCOUNT in the last scan. This will be taken as the previous count of errors. We will again scan the file and will count the errors again and this new count will be fCOUNT.
Means the fCOUNT in the last count has become iCOUNT. This iCOUNT will be compared with the new fCOUNT.
#!/bin/sh
if [ ! -f a.log ]; then
touch a.log
fi
iCOUNT=`grep -c ERROR a.log`
fCOUNT=`grep -c ERROR b.log`
if [ ${fCOUNT} -gt ${iCOUNT} ]; then
echo "1"
else
echo "0"
fi
$fCOUNT > c.log
exit
something like this. We have to compare again & again the previous count because new logs are getting added repeatedly and consequently new errors will be there .
3) Let us suppose we scan the file right now . And suppose this gives us a count of ERROR as 5 . We will assume this as initial count.
4) After 1 hour we will scan the same log file once again. Let us suppose this gives the count as 10. Now we have to compare this 10 with the last count 5 .
5) Now "10" is the final count and the "5" is the initial count.
6) In the next scan , "10" will become initial count and some new number will become the final count.
7) As we created the "a.log" file earlier. I want to move "10" to this "a.log". So, that I can compare in the below mention way
#!/bin/sh
if [ ! -f a.log ]; then
touch a.log
iCOUNT=`a.log | wc -l`
fCOUNT=`grep -c ERROR b.log`
if [ ${fCOUNT} -gt ${iCOUNT} ]; then
echo "1"
else
echo "0"
fi
$fCOUNT > a.log
else
iCOUNT=`a.log | wc -l`
fCOUNT=`grep -c ERROR b.log`
if [ ${fCOUNT} -gt ${iCOUNT} ]; then
echo "1"
else
echo "0"
fi
fi
a.log will have only the number . I guess I am making a mistake in
Say your filename is a.log
For the first run, iCOUNT and fCOUNT will be same. Hence o/p will be 0.
Script will store the fCOUNT to tmp_file.log and in next run, it will pick iCOUNT from this file. Try following code.
#!/bin/sh
if [ ! -f a.log ]; then
touch a.log
fi
fCOUNT=`grep -c ERROR a.log`
if [ ! -f tmp_file.log ]; then
iCOUNT=$fCOUNT
else
read iCOUNT < tmp_file.log
fi
if [ ${fCOUNT} -gt ${iCOUNT} ]; then
echo "1"
else
echo "0"
fi
echo $fCOUNT > tmp_file.log