Posted By


ksaver on 08/22/10

Tagged


Statistics


Viewed 675 times
Favorited by 2 user(s)

progress_bar.sh


/ Published in: Bash
Save to your folder(s)



Copy this code and paste it in your HTML
  1. # snippet: progress_bar.sh
  2. # version: 0.02
  3. # author: ksaver (at identi.ca)
  4. # license: http://creativecommons.org/licenses/BSD/
  5. # sets: progress_bar function
  6. # arguments: progress_bar function: optional arguments "Text" and width.
  7. # "Text": Any informative text string.
  8. # Width: An integer number, is the max width for the progress bar.
  9. # usage: Use into a script:
  10. # source progress_bar.sh
  11. # a) progress_bar "Counting:" 25 <<< $PERCENT
  12. # b) for i in {1..100}; do echo $i; sleep 1; done |progress_bar "" 20
  13. # output: Counting: [ ============> ] 50%
  14.  
  15. function progress_bar()
  16. {
  17. while read PERCENT
  18. do
  19. COUNT=0
  20. FOO='>'
  21. BAR=''
  22. CHAR='=' # Change by "#", "|" or any character.
  23. TEXT=${1:-"Progress:"} # Sets default value to var $TEXT
  24. WIDTH=${2:-40} # Sets default value to var $WIDTH
  25.  
  26. PROGRESS=$(($PERCENT*$WIDTH/100))
  27.  
  28. while [ $COUNT -lt $(($PROGRESS+1)) ]
  29. do
  30. printf "\r\t$TEXT [ %s%s ] $PERCENT%% " ${BAR} ${FOO}
  31.  
  32. BAR="${BAR}$CHAR"
  33. COUNT=$(($COUNT+1))
  34. done
  35. done
  36. echo " Done."
  37. }

URL: http://identi.ca/ksaver

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.