Operating System - HP-UX
1831309 Members
3204 Online
110023 Solutions
New Discussion

generating delay based on the counter value

 
SOLVED
Go to solution
Anand_30
Regular Advisor

generating delay based on the counter value

Hi,

I have the following script:

c=1
while [ $c -le 3000 ]
do
cat ${myfile}|while read A B
do
"something"
c=`expr $c + 1`
echo $c
done
done

I want to generate 1 sec delay for every 200th count of the counter. Can anyone please help me in accomplishing the task.

Thanks,
Andy

2 REPLIES 2
Steven E. Protter
Exalted Contributor
Solution

Re: generating delay based on the counter value

c=1
while [ $c -le 3000 ]
do
cat ${myfile}|while read A B
do
"something"
c=`expr $c + 1`

if [ $c -eq 200 ]
then
sleep 1
fi

echo $c
done
done

You can reset the counter if you intent is to do that or you can do some division to see if the number is divisible by 200. You you can set a second counter like this.

c=1
d=1
while [ $c -le 3000 ]
do
cat ${myfile}|while read A B
do
"something"
c=`expr $c + 1`
d=`expr $d + 1`

if [ $d -eq 200 ]
then
d=1
sleep 1
fi

echo $c
done
done


Do note that this expression type is considered obsolete:

d=`expr $d + 1`

It should be d=$(expr $d + 1)'

For new scripts.
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Seth Parker
Trusted Contributor

Re: generating delay based on the counter value

I've always been fond of the modulus, so how about this:

c=1
while [ $c -le 3000 ]
do
cat ${myfile}|while read A B
do
"something"
c=$(expr $c + 1)
[ $(expr $c % 200) -eq 0 ] && sleep 1
echo $c
done
done

This way, the sleep only happens when the remainder of $c / 200 is 0.

Regards,
Seth