Home | VizagInfo Mirror of LG

[ Table Of Contents ][ Answer Guy Current Index ] greetings   Meet the Gang   1   2   3   4   5   6   7   8   9 [ Index of Past Answers ]

(?) The Answer Gang (!)

By Jim Dennis, Ben Okopnik, Dan Wilder, Breen, Chris, and the Gang, the Editors of Linux Gazette... and You!
Send questions (or interesting answers) to

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

From 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

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:
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.)

(!) [Thomas] But, I prefer your script!! :-)

(?) <grin> 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.

(!) [Thomas] ....and they did just that :-). I believe that on RedHat systems, it was supplied as part of the "initscripts" rpm, thus:
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. :) 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 "", either - although there's an interesting-looking "nanosleep".

This page edited and maintained by the Editors of Linux Gazette Copyright © 2001
Published in issue 73 of Linux Gazette December 2001
HTML script maintained by Heather Stern of Starshine Technical Services,

[ Table Of Contents ][ Answer Guy Current Index ] greetings   Meet the Gang   1   2   3   4   5   6   7   8   9 [ Index of Past Answers ]