# while do done loop problem

**URL:** <https://community.unix.com/t/while-do-done-loop-problem/224683>\
**Category:** Shell Programming and Scripting\
**Created:** [December 17, 2008, 12:26am UTC](https://community.unix.com/t/while-do-done-loop-problem/224683 "2008-12-17T00:26:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![himvat](https://community.unix.com/letter_avatar/himvat/32/5_5575768a8748004e209b776fc1b2916d.png) [@himvat](https://community.unix.com/u/himvat)\
**Post date:** [December 17, 2008, 12:26am UTC](https://community.unix.com/t/while-do-done-loop-problem/224683/1 "2008-12-17T00:26:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![manikantants](https://community.unix.com/letter_avatar/manikantants/32/5_5575768a8748004e209b776fc1b2916d.png) [@manikantants](https://community.unix.com/u/manikantants)\
**Post date:** [December 17, 2008, 12:37am UTC](https://community.unix.com/t/while-do-done-loop-problem/224683/2 "2008-12-17T00:37:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![himvat](https://community.unix.com/letter_avatar/himvat/32/5_5575768a8748004e209b776fc1b2916d.png) [@himvat](https://community.unix.com/u/himvat)\
**Post date:** [December 17, 2008, 1:01am UTC](https://community.unix.com/t/while-do-done-loop-problem/224683/3 "2008-12-17T01:01:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![manikantants](https://community.unix.com/letter_avatar/manikantants/32/5_5575768a8748004e209b776fc1b2916d.png) [@manikantants](https://community.unix.com/u/manikantants)\
**Post date:** [December 17, 2008, 1:24am UTC](https://community.unix.com/t/while-do-done-loop-problem/224683/4 "2008-12-17T01:24:43Z")

</div>

am still not clear with the requirment.

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

---

<div class="post-metadata">

**Author:** ![himvat](https://community.unix.com/letter_avatar/himvat/32/5_5575768a8748004e209b776fc1b2916d.png) [@himvat](https://community.unix.com/u/himvat)\
**Post date:** [December 17, 2008, 1:50am UTC](https://community.unix.com/t/while-do-done-loop-problem/224683/5 "2008-12-17T01:50:11Z")

</div>

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.

```nohighlight

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

---

<div class="post-metadata">

**Author:** ![himvat](https://community.unix.com/letter_avatar/himvat/32/5_5575768a8748004e209b776fc1b2916d.png) [@himvat](https://community.unix.com/u/himvat)\
**Post date:** [December 17, 2008, 2:09am UTC](https://community.unix.com/t/while-do-done-loop-problem/224683/6 "2008-12-17T02:09:57Z")

</div>

kindly advice ...how to proceed further ??

---

<div class="post-metadata">

**Author:** ![manikantants](https://community.unix.com/letter_avatar/manikantants/32/5_5575768a8748004e209b776fc1b2916d.png) [@manikantants](https://community.unix.com/u/manikantants)\
**Post date:** [December 17, 2008, 2:22am UTC](https://community.unix.com/t/while-do-done-loop-problem/224683/7 "2008-12-17T02:22:08Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![himvat](https://community.unix.com/letter_avatar/himvat/32/5_5575768a8748004e209b776fc1b2916d.png) [@himvat](https://community.unix.com/u/himvat)\
**Post date:** [December 17, 2008, 3:14am UTC](https://community.unix.com/t/while-do-done-loop-problem/224683/8 "2008-12-17T03:14:20Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![manikantants](https://community.unix.com/letter_avatar/manikantants/32/5_5575768a8748004e209b776fc1b2916d.png) [@manikantants](https://community.unix.com/u/manikantants)\
**Post date:** [December 17, 2008, 3:51am UTC](https://community.unix.com/t/while-do-done-loop-problem/224683/9 "2008-12-17T03:51:06Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![himvat](https://community.unix.com/letter_avatar/himvat/32/5_5575768a8748004e209b776fc1b2916d.png) [@himvat](https://community.unix.com/u/himvat)\
**Post date:** [December 17, 2008, 4:54am UTC](https://community.unix.com/t/while-do-done-loop-problem/224683/10 "2008-12-17T04:54:35Z")

</div>

```nohighlight
 
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

---

<div class="post-metadata">

**Author:** ![manikantants](https://community.unix.com/letter_avatar/manikantants/32/5_5575768a8748004e209b776fc1b2916d.png) [@manikantants](https://community.unix.com/u/manikantants)\
**Post date:** [December 17, 2008, 5:00am UTC](https://community.unix.com/t/while-do-done-loop-problem/224683/11 "2008-12-17T05:00:51Z")

</div>

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
