/ Published in: Bash
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
# snippet: progress_bar.sh # version: 0.02 # author: ksaver (at identi.ca) # license: http://creativecommons.org/licenses/BSD/ # sets: progress_bar function # arguments: progress_bar function: optional arguments "Text" and width. # "Text": Any informative text string. # Width: An integer number, is the max width for the progress bar. # usage: Use into a script: # source progress_bar.sh # a) progress_bar "Counting:" 25 <<< $PERCENT # b) for i in {1..100}; do echo $i; sleep 1; done |progress_bar "" 20 # output: Counting: [ ============> ] 50% function progress_bar() { while read PERCENT do COUNT=0 FOO='>' BAR='' CHAR='=' # Change by "#", "|" or any character. TEXT=${1:-"Progress:"} # Sets default value to var $TEXT WIDTH=${2:-40} # Sets default value to var $WIDTH PROGRESS=$(($PERCENT*$WIDTH/100)) while [ $COUNT -lt $(($PROGRESS+1)) ] do printf "\r\t$TEXT [ %s%s ] $PERCENT%% " ${BAR} ${FOO} BAR="${BAR}$CHAR" COUNT=$(($COUNT+1)) done done echo " Done." }