# Complex loop...

**URL:** <https://community.unix.com/t/complex-loop/158140>\
**Category:** Shell Programming and Scripting\
**Created:** [December 12, 2005, 10:55am UTC](https://community.unix.com/t/complex-loop/158140 "2005-12-12T10:55:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgirinath](https://community.unix.com/letter_avatar/mgirinath/32/5_5575768a8748004e209b776fc1b2916d.png) [@mgirinath](https://community.unix.com/u/mgirinath)\
**Post date:** [December 12, 2005, 10:55am UTC](https://community.unix.com/t/complex-loop/158140/1 "2005-12-12T10:55:00Z")

</div>

Hi,

I did a lot of search for this but I could not find it, so I am posting it here....Please help.

I have a situation where I need to do polling or sleep for a while...here is what I have..

#! /usr/bin/sh  
job1 runs \> result1.txt  
RESULT1\_CNT=`wc -l result1.txt | awk '{ print $1 }'`  
if[RESULT1\_CNT -gt 0]  
then  
while [RESULT1\_CNT -gt 0]  
do  
JOb2 runs  
RESULT1\_CNT=RESULT1\_CNT-1  
done  
else  
#This is the part I am struggling for....  
#here it should sleep for specified time and run job1 again...  
#if the RESULT1\_CNT is not greater than 0 three consecutive times  
#then it should exit

any help is appreciated...

Thanks,

Girinath.

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/5_5575768a8748004e209b776fc1b2916d.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [December 12, 2005, 12:19pm UTC](https://community.unix.com/t/complex-loop/158140/2 "2005-12-12T12:19:42Z")

</div>

This is a start:

```nohighlight
#! /usr/bin/ksh

let consecutive=-1
while true
do
    job1 runs > result1.txt
    RESULT1_CNT=`wc -l result1.txt | awk '{ print $1 }'`
    if [$RESULT1_CNT -lt 1]; then
        let consecutive=$consecutive+1
        if [$consecutive -eq 2]; then # note: -1 + 3 = 2
             exit 1                  
        fi 
    else
        let consecutive=-1
    fi
    if[$RESULT1_CNT -gt 0]; then
        while [RESULT1_CNT -gt 0]
        do
           JOB2 runs
           RESULT1_CNT=RESULT1_CNT-1
        done
    fi
    sleep 60
done

```

Note that I used ksh...
