bash progress bar snippet

little snippet of bash that produces a progress bar

    #!/bin/bash
    # A simple progress bar that overwrites itsself in place

function print_progress_bar {
    cols=`tput cols`
    jobName="$1"
    doneNum="$2"
    totalNum="$3"

    jobLen=${#jobName}
    doneLen=${#doneNum}
    totalLen=${#totalNum}

    barLength=$(($cols - ($jobLen+$doneLen+totalLen+14)))

    percentDone=`echo "scale=1; ($doneNum/$totalNum)*100" | bc`

    echo -en "\r[$jobName] $doneNum/$totalNum ["
    barStars=`echo "scale=2; (($barLength/100)*$percentDone)" | bc`
    barStars=`echo $barStars | cut -d "." -f1`
    emptyStars=$(($barLength-$barStars))

    for ((i=0; i<$barStars; i++)) do printf "%s" "="; done
    for ((i=0; i<$emptyStars; i++)) do printf "%s" " "; done

    echo -n "] $percentDone%"
}

print_progress_bar testing 1 10
sleep 0.2
print_progress_bar testing 2 10
sleep 0.2
print_progress_bar testing 5 10
sleep 0.2
print_progress_bar testing 6 10
sleep 0.2
print_progress_bar testing 7 10
sleep 0.2
print_progress_bar testing 8 10
sleep 0.2
print_progress_bar testing 9 10
sleep 0.2
print_progress_bar testing 10 10
sleep 0.2

blogroll

social