Skip to main content
4 of 6
Fix code block formatting
Jander
  • 17.1k
  • 6
  • 56
  • 67

To convert from fractions to decimals in bash, do something like

myvar=`echo "scale=4; 5/10" | bc`

Then, to do a loop on that value,

for i in `seq 1 1000`; do sleep $myvar; done

My sleep implementation on Debian (GNU) seem to accept decimal sleep values.

Unfortunately..

With that kind of precision (4-5 decimal places), you're going to want something like a perl script or a compiled program; the overhead of calling any program within the loop is going to add a lot of jitter. Calling sleep itself will take a few milliseconds.

Consider the following, a 1/10,000ths of a second sleep, done 1000 times:

time for i in `seq 1 1000`; do sleep 0.0001; done

real    0m2.099s
user    0m3.080s
sys     0m1.464s

The expected result would be 1/10th of a second. Sleep has nowhere near the tolerances you want.

http://stackoverflow.com/questions/896904/how-do-i-sleep-for-a-millisecond-in-perl

using perl's Time::HiRes, 1000*1000 microseconds:

my $i=0;
for($i=0;$i<=1000;$i++) {
        usleep(1000);
}

real    0m1.133s
user    0m0.024s
sys     0m0.012s

gives us much closer to a second.

Rob Bos
  • 4.6k
  • 1
  • 14
  • 13