I've been wondering about this one for some time: Is there an alternative to sleep in bash?
The reason: I'd like to simply limit the amount of processor usage in continuous while : script scenarios without spawning endless sleep processes as well. After beating the manpages, I still haven't a clue as to how one might do something like this...
I guess you have an older bash then. Your only options are to switch to a language with more power or just live with the sleep processes. The sleep processes would not bother me and I don't know why you worry about them.
So ksh, newer bash, or something like perl would all do what you want.
Bit of lateral thinking here.
How about using the sound card as your delay; ensure it is enabled at maximum volume.
Some demo code to show as an example, OSX 10.7.5 default bash terminal, just
comment out the "afplay" lines and uncomment the "aplay" lines for Ubuntu:-
#!/bin/bash --posix
# timer
# *********************************************************
# Generate a 1 second delay and beep.
# Issued as Public Domain, CC0. You may do with it as you please.
# *********************************************************
if [ ! -e "/tmp/sinewave.wav" ]
then
> /tmp/sinewave.wav
printf "\x52\x49\x46\x46\x64\x1F\x00\x00\x57\x41\x56\x45\x66\x6D\x74\x20\x10\x00\x00\x00\x01\x00\x01\x00\x40\x1F\x00\x00\x40\x1F\x00\x00\x01\x00\x08\x00\x64\x61\x74\x61\x40\x1F\x00\x00" >> /tmp/sinewave.wav
cp /tmp/sinewave.wav /tmp/timer.wav
for n in {0..999}
do
printf "\x80\x26\x00\x26\x7F\xD9\xFF\xD9" >> /tmp/sinewave.wav
printf "\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F" >> /tmp/timer.wav
done
fi
# *********************************************************
# The line below uses various Linux flavours, "aplay"...
# aplay /tmp/sinewave.wav
# *********************************************************
# Use the OSX default basic command line audio player, "/usr/bin/afplay".
afplay /tmp/sinewave.wav
# aplay /tmp/sinewave.wav
# A demo delay of around 6 seconds, about 1.5 seconds per loop.
for n in {1..4}
do
# aplay /tmp/timer.wav
afplay /tmp/timer.wav
done
afplay /tmp/sinewave.wav
# aplay /tmp/sinewave.wav
# *********************************************************
exit 0
# Enjoy finding simple solutions to often very difficult problems...
Results...
Last login: Thu Mar 13 08:19:23 on ttys000
AMIGA:barrywalker~> chmod 755 timer
AMIGA:barrywalker~> ./timer
AMIGA:barrywalker~> ./timer &
[1] 247
AMIGA:barrywalker~> _
You have the added advantage of having a 1 second pure sinewave burst too!
Well it __might__ be useful to systems that do not have the "sleep" command.
Android for example.
As it is a wave file the principle could be used to create a function generator
system on just about any platform and use the default player on said platform
as the signal source.
Already working on my BlackBerry 'phone ready for calibrating the AudioScope...
This snippet was just a side effect that could be useful...
Thanks, wisecracker, for those keen insights. It'll take me a bit to unravel & apply, but I know I learned something useful here
Found the solution to the "read -t" issue in my bumblings about.
This works :):
#!/bin/bash
while :
do
echo pausing...
read -t 2
done
Indeed, Ubuntu loves a certain incarnation of dash; which, for some reason, is averse to "read." Nasty habit, that /bin/sh bangline...
(FWIW, I was given to understand bash would automagically "step in" when situations such as this came up (as is done in Parted Magic), hence the default /sh bang usage...)
At any rate, and if folks could humor me a moment longer, how might we go about forming a commandline to point read at empty space (via -u, perhaps?) as opposed to taking user input? Possible???
This is because /bin/sh gets you "a Bourne shell" but not any particular Bourne shell. What shell you get depends on what shell your system considers a good default. If you don't use extended shell features #!/bin/sh will work on most systems (except perhaps Solaris where /bin/sh can be really, really old.)
So, if you write bash code, you should use #!/bin/bash. If you write ksh code, you should use #!/bin/ksh. #!/bin/sh should only be used for generic Bourne code.