|  ![[ Table Of Contents ]](../../gx/navbar/toc.jpg) ![[ Answer Guy Current Index ]](../../gx/dennis/answertoc.jpg) |  Meet the Gang  
  1  
  2  
  3  
  4  
  5  
  6  
  7  
  8  
  9 | ![[ Index of Past Answers ]](../../gx/dennis/answerpast.jpg)  | 
 The Answer Gang
	The Answer Gang
	 
There is no guarantee that your questions here will ever be answered. Readers at confidential sites must provide permission to publish. However, you can be published anonymously - just let us know!
TAG Member bios | FAQ | Knowledge base
 fine-grained delay in shell scripts
fine-grained delay in shell scriptsFrom Ben Okopnik
Answered By Thomas Adam, Mike Orr, John Karns
So, you're writing a shell script, and you want to add a little pizzazz: you know, a little blinking indicator, or a slow display that prints out "Please wait" one letter at a time or something. You look around, and the only choices you've got are a) "sleep" (a minimum 1-second delay), or various strange things involving loops and "cat"ting large files (which makes you CPU usage shoot up into the 90% range.) Blechhh. What's a poor but honest scripter to do?
Farm the job out, of course.
See attached nap.pl.txt
It doesn't get much simpler. "nap" will give you a delay in milliseconds, plus a tiny machine-dependent fudge factor for starting Perl. Here, as an example, is that famous "rotating dash" indicator, using "nap":
while :; do for x in - \\ \| /; do printf "%c\b" $x; nap 100; done; done
[Thomas Adam] Tut tut Ben. For this kind of use, I always tweak the millisecond usage of the command:
"usleep"
Then I can use a for i in....loop and a usual "echo" in Bash.
Works everytime.
But, I prefer your script!!
 OK, I'll admit my ignorance - what's a "usleep"? There's nothing like that
on my system, or indeed in the Debian "Contents-i386.gz" file list. Please
enlighten me. (I do seem to _vaguely_ remember something like that in C,
but that's less than helpful.)
 
OK, I'll admit my ignorance - what's a "usleep"? There's nothing like that
on my system, or indeed in the Debian "Contents-i386.gz" file list. Please
enlighten me. (I do seem to _vaguely_ remember something like that in C,
but that's less than helpful.)
[Thomas] But, I prefer your script!!

 
 
 Well, you got _something_ useful out of it. That's a plus.
 Well, you got _something_ useful out of it. That's a plus.
[Mike] ..From "man 3 usleep": "The usleep() function suspends execution of the calling process for usec microseconds."
It looks like it's available only as a C function. Somebody should wrap it up in a command.
 <smirk> I did.
 
<smirk> I did.
[Thomas] ....and they did just that
. I believe that on RedHat systems, it was supplied as part of the "initscripts" rpm, thus:
http://www.rpmfind.net//linux/RPM/redhat/7.2/i386/initscripts-6.40-1.i386.html
/sbin/usleep
is where my copy resides (despite the fact im running SuSE 7.1 professional).
Hope that helps
[John K] No such animal on my SuSE 7.2 install ...
jkarns@jkInsp8000:~ > locate usleep /home/jkarns/Dwnlds/Linux/XScreenSavers/xscreensaver-3.32/utils/usleep.c /home/jkarns/Dwnlds/Linux/XScreenSavers/xscreensaver-3.32/utils/usleep.h /home/jkarns/Dwnlds/Linux/XScreenSavers/xscreensaver-3.32/utils/usleep.o /usr/share/doc/packages/mod_php/doc/function.usleep.html /usr/share/doc/packages/phpdoc/manual/function.usleep.html /usr/share/man/allman/man3/usleep.3.gz /usr/share/man/man3/usleep.3.gz
 As I'd mentioned, it's not part of Debian - whereas Perl is in every
distro. I'm sticking with portability.
 
As I'd mentioned, it's not part of Debian - whereas Perl is in every
distro. I'm sticking with portability. 
 Besides, when would you ever need
microsecond precision in a shell script?
Even milliseconds is splitting it frog-hair fine.
 Besides, when would you ever need
microsecond precision in a shell script?
Even milliseconds is splitting it frog-hair fine.
[Mike] You don't, but sometimes you want to delay for a quarter second or half a second.
 BTW, "usleep" isn't described in "libc.info.gz", either - although there's
an interesting-looking "nanosleep".
 
BTW, "usleep" isn't described in "libc.info.gz", either - although there's
an interesting-looking "nanosleep".
|  ![[ Table Of Contents ]](../../gx/navbar/toc.jpg) ![[ Answer Guy Current Index ]](../../gx/dennis/answertoc.jpg) |  Meet the Gang  
  1  
  2  
  3  
  4  
  5  
  6  
  7  
  8  
  9 | ![[ Index of Past Answers ]](../../gx/dennis/answerpast.jpg)  |