I would like to have a while loop in bash like this:

while read i
do
    ~/bin/submit_job $i
    sleep N
done

But I would like N in sleep N to be short at the beginning, then progressively increase after the first few loops. Something like one second for the first 8, then increase in seconds like this:

 1 1 1 1 1 1 1 1 (first 8 iterations)
 2 2 2 2 2 2 2 2 
 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 
 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 
 ...

Any ideas?

link|improve this question

73% accept rate
feedback

1 Answer

up vote 3 down vote accepted

Tested and working:

#/bin/bash
sleeptime=1
countsleeps=1
maxcount=8

while read i
do
    ~/bin/submit_job $i
    sleep $sleeptime
    let countsleeps++
    if ((countsleeps>maxcount))
    then
        countsleeps=1
        let sleeptime*=2
        if ((sleeptime>2)) ; then let maxcount*=2 ; fi
    fi
done
link|improve this answer
bash: let: 1++: syntax error: operand expected (error token is "+") – 130490868091234 Feb 7 at 14:08
That should probably be let countsleeps++ – FatalError Feb 7 at 14:19
Fixed, thanks both. – choroba Feb 7 at 14:22
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.