Skip to content

Instantly share code, notes, and snippets.

@Gisleburt
Last active January 2, 2016 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gisleburt/b7e346268982355e54d1 to your computer and use it in GitHub Desktop.
Save Gisleburt/b7e346268982355e54d1 to your computer and use it in GitHub Desktop.
Show a rotating cursor while running long commands
#!/bin/bash
# Spinner Source: http://it.toolbox.com/blogs/locutus/bash-bits-nibbles-and-bytes-a-rotating-cursor-while-you-wait-22867
# Parallel process Source: http://stackoverflow.com/questions/6384013/run-bash-commands-in-parallel-track-results-and-count#6384467
# Hide job status source: http://superuser.com/questions/377775/how-do-i-keep-background-jobs-from-telling-me-theyre-done
# Spin the spinner. Must be called repeatedly to animate
function spin() {
case $toggle
in
1)
echo -n " \ "$1
echo -ne "\r"
toggle="2"
;;
2)
echo -n " | "$1
echo -ne "\r"
toggle="3"
;;
3)
echo -n " / "$1
echo -ne "\r"
toggle="4"
;;
*)
echo -n " - "$1
echo -ne "\r"
toggle="1"
;;
esac
}
# Close off the spinner
function complete() {
echo \* $1 [$?]
}
# Run a command and spin the spinner
function run() {
command=${1}
(sleep 1 && $command) 2> errors.log & disown
pid1=$!
running1=`ps -p $pid1 --no-headers | wc -l`
while ( [ $running1 != 0 ] ) do
spin "$command"
sleep 1
running1=`ps -p $pid1 --no-headers | wc -l`
done
complete "$command"
}
#usage
command="command constructed as a string"
run $command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment