Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

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
share|improve this answer
    
bash: let: 1++: syntax error: operand expected (error token is "+") –  130490868091234 Feb 7 '12 at 14:08
    
That should probably be let countsleeps++ –  FatalError Feb 7 '12 at 14:19
    
Fixed, thanks both. –  choroba Feb 7 '12 at 14:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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