while do done loop problem

#!/bin/sh
findit=`find . -name "a.log" | wc -l`
if [ $findit -eq 0 ]; then
   touch a.log
fi
iCOUNT=`grep ERROR a.log | wc -l`
while (1)
do
   fCOUNT=`grep ERROR b.log | wc -l`
   if [ ${fCOUNT} -gt ${iCOUNT} ]; then
      echo "1"
   else
      echo "0"
   fi
done
 

Hi ,

I am trying to run the above mentioned script, but it is giving infinite response "1". It is not coming out of the loop.

please advice

It is an infinite loop. There is no break in that loop.
You may break the loop by checking some condition.

And I don't understand why a loop is required there.
Pls let us know what is the requirment.

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".

am still not clear with the requirment.

#!/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

Try above code

YES !! it worked.

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 .

kindly advice ...how to proceed further ??

need to confirm few things.
For the first time:

(1) from which file fcount should be picked up?
(2) from which file icount should be picked up?

For sub sequent run:

(1) from which file fcount should be picked up?
(2) from which file icount should be picked up?

1) There is only one file to be scaned

2)This file will be scaned after every 1 hour.

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

iCOUNT=`a.log | wc -l`

Please Advice

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

 
read iCOUNT < tmp_file.log
 
echo $fCOUNT > tmp_file.log
 
iCOUNT=$fCOUNT
 

I would really appreciate if you could explain the purposes of the above mentiond commands

Regards

read iCOUNT < tmp_file.log ---> Reads content of tmp_file.log to iCOUNT variable
echo $fCOUNT > tmp_file.log --> Writing fCOUNT to tmp_file.log
iCOUNT=$fCOUNT --> Assigning / Copying value at fCOUNT to iCOUNT