DaniWeb IT Discussion Community

Code Snippets (http://www.daniweb.com/code/)
-   bash (http://www.daniweb.com/code/bash.html)
-   -   Spinner (http://www.daniweb.com/code/snippet699.html)

Stylish bash syntax
May 9th, 2007
Here is a little spinner thing for the command line, used when your current bash script is waiting on a background/system process.

The weird character is ^H or a backspace.

  1. ## This is similar to what FreeBSD does when it is booting
  2. ### some command &
  3. ### do_progress_spin_l $!
  4. # A small "asterik" like thing will spin until the process is complete
  5. #+ (when ps ax | grep PID no longer finds anything)
  6. do_progress_spin_l()
  7. {
  8. ## The characters to use
  9. # These will be overlapped via ^H
  10. ICON[0]="/"
  11. ICON[1]="-"
  12. ICON[2]="\\"
  13. ICON[3]="|"
  14.  
  15. ## The process we wish to monitor
  16. PROCESS=$1
  17.  
  18. ## Initialize count value
  19. # print out first icon
  20. icon_num=0
  21. echo -n ${ICON[$icon_num]}
  22.  
  23. ## While the process exists in ps ax, do this loop
  24. while ( ps ax | grep $PROCESS | grep -v "grep" 1>/dev/null )
  25. do
  26. ## If our count is bigger than 3 (our highest number icon)
  27. # reset it to 0
  28. if [ "$icon_num" -gt "3" ]; then
  29. icon_num=0
  30. fi
  31.  
  32. ## ^Hnew_icon
  33. echo -n ${ICON[$icon_num]}
  34. icon_num=`expr $icon_num + 1`
  35.  
  36. sleep 0.5
  37. # sleep 1
  38. done
  39.  
  40. ## Little clean-up. Print done and a new line
  41. echo -n "... Done"
  42. echo ""
  43. }