Posted By

ksaver on 08/22/10


Tagged

bar Bash progress ProgressBar


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

ksaver
vkolev


progress_bar.sh


 / Published in: Bash
 

URL: http://identi.ca/ksaver

  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. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: ksaver on August 22, 2010

! /usr/bin/env bash

a simple timer example.

function timer() { source progressbar.sh LIMIT=300 #5 mins. COUNT=0 while [ $COUNT -lt $LIMIT ] do echo $(($COUNT*100/$LIMIT)) #%percent let COUNT=$COUNT+1 sleep 1 done | progressbar return $? }

timer

Posted By: ksaver on August 22, 2010

Example of usage: Simple Timer: http://pastebin.com/TU5evdHx

You need to login to post a comment.