Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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 "+") – 719016 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .