Spinner Function for Shell Scripts
Devin Teske
dteske at vicor.com
Fri Nov 12 01:26:34 UTC 2010
One-line fix for a small bug (see below).
On Thu, 2010-11-11 at 12:12 -0800, Devin Teske wrote:
> Hi fellow hackers... I come with baring gifts!
>
> So, just as the subject-line says, ... here's an efficient and robust
> spinner function compatible with many shells.
>
> But, before I get into the code,
>
> [snip]
>
> eval_spin()
> {
> local commands=
> if [ $# -gt 0 ]; then
> # Take commands from positional arguments
> while [ $# -gt 0 ]; do
> commands="$commands${commands:+ }'$1'"
> shift 1
> done
> else
> # Take commands from standard input
> while read -r LINE; do
> commands="$commands
> $LINE"
> done
> fi
> [ "$commands" ] || return $SUCCESS
>
> #
> # Execute the command w/ spinner
> #
> (
> eval "$commands" > /dev/null 2>&1
The above line should instead be:
( eval "$commands" ) > /dev/null 2>&1
To protect from things such as: eval_spin exit
> echo $?
> ) | (
> n=1
> spin="/-\\|"
> DONE=
>
> echo -n " "
> while [ ! "$DONE" ]; do
> DONE=$( /bin/sh -c 'read -t 0 DONE; echo $DONE' )
> printf "\b%s" $( echo "$spin" | sed -e \
> "s/.\{0,$(( $n % ${#spin} ))\}\(.\).*/\1/" )
> n=$(( $n + 1 ))
> done
> printf "\b \b"
> exit $DONE
> )
> }
>
> [snip]
--
Cheers,
Devin Teske
-> CONTACT INFORMATION <-
Business Solutions Consultant II
FIS - fisglobal.com
510-735-5650 Mobile
510-621-2038 Office
510-621-2020 Office Fax
909-477-4578 Home/Fax
devin.teske at fisglobal.com
-> LEGAL DISCLAIMER <-
This message contains confidential and proprietary information
of the sender, and is intended only for the person(s) to whom it
is addressed. Any use, distribution, copying or disclosure by any
other person is strictly prohibited. If you have received this
message in error, please notify the e-mail sender immediately,
and delete the original message without making a copy.
-> END TRANSMISSION <-
More information about the freebsd-hackers
mailing list