Return to Snippet

Revision: 30798
at August 22, 2010 16:51 by ksaver


Updated Code
#  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."
}

Revision: 30797
at August 22, 2010 16:46 by ksaver


Updated Code
#  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
#             progress_bar "Counting:" 25 <<< $PERCENT
#             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."
}

Revision: 30796
at August 22, 2010 16:21 by ksaver


Initial Code
#  snippet:   progress_bar.sh
#  version:   0.02
#  author:    ksaver (at identi.ca)
#  licence:   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
#             progress_bar "Counting:" 25 <<< $PERCENT
#             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."
}

Initial URL
http://identi.ca/ksaver

Initial Description


Initial Title
progress_bar.sh

Initial Tags
Bash

Initial Language
Bash