Skip to content

Instantly share code, notes, and snippets.

@macsmax
Last active October 6, 2021 17:01
Show Gist options
  • Save macsmax/0408b74cf1c707ffc31941028debd406 to your computer and use it in GitHub Desktop.
Save macsmax/0408b74cf1c707ffc31941028debd406 to your computer and use it in GitHub Desktop.
bash progress bar function
#!/bin/bash
# Function to print a progress barr, pass three arguments: total counter, current counter, start epoch
function progressbar {
TOT_COUNT=${1}
COUNTER=${2}
STARTEPOCH=${3}
CUREPOCH=`date +%s`
# get the percentage
let CUR_PERC=$COUNTER*100/$TOT_COUNT
# get the elapsed seconds
let DELTA_SECONDS=$CUREPOCH-$STARTEPOCH
let DELTA_SECONDS_MOD=$DELTA_SECONDS%60
let DELTA_MINUTES=$DELTA_SECONDS/60
let DELTA_MINUTES_MOD=$DELTA_MINUTES%60
let DELTA_HOURS=$DELTA_MINUTES/60
let DELTA_HOURS_MOD=$DELTA_HOURS%60
# get the estimated end time
let EST_SECONDS=$DELTA_SECONDS*$TOT_COUNT/$COUNTER
let REMAINING_SECONDS_MOD=$EST_SECONDS%60
let REMAINING_MINUTES=$EST_SECONDS/60
let REMAINING_MINUTES_MOD=$REMAINING_MINUTES%60
let REMAINING_HOURS=$REMAINING_MINUTES/60
let REMAINING_HOURS_MOD=$REMAINING_HOURS%60
# get the eta time going to 0
let ETA_SECONDS=$EST_SECONDS-$DELTA_SECONDS
let ETA_SECONDS_MOD=$ETA_SECONDS%60
let ETA_MINUTES=$ETA_SECONDS/60
let ETA_MINUTES_MOD=$ETA_MINUTES%60
let ETA_HOURS=$ETA_MINUTES/60
let ETA_HOURS_MOD=$ETA_HOURS%60
# how many dashes out of 20
let DASHES_PERC=$CUR_PERC/5
let DASHES=$(printf "%d" $DASHES_PERC)
let WHITESPACES=21-$DASHES
PROGRESSBARR_D=$(awk -v i=$DASHES 'BEGIN { OFS="#"; $i="#"; print }')
PROGRESSBARR_W=$(awk -v i=$WHITESPACES 'BEGIN { OFS=" "; $i=" "; print }')
PROGRESSBARR="${PROGRESSBARR_D}${PROGRESSBARR_W}"
runtime=$(printf "Runtime: %02d:%02d:%02d" $DELTA_HOURS_MOD $DELTA_MINUTES_MOD $DELTA_SECONDS_MOD)
completion=$(printf "Completion: %02d:%02d:%02d" $REMAINING_HOURS_MOD $REMAINING_MINUTES_MOD $REMAINING_SECONDS_MOD)
etatime=$(printf "ETA: %02d:%02d:%02d" $ETA_HOURS_MOD $ETA_MINUTES_MOD $ETA_SECONDS_MOD)
echo -ne "${PROGRESSBARR}(${CUR_PERC}%) ${runtime} ${completion} ${etatime}\r"
}
startepoch=`date +%s`
counter=1
total=1000
for i in `seq 1 1000`; do
sleep 0.1 # do something here rather than sleep and redirect everything to a file eg.: >> filename.log 2&>1
progressbar "$total" "$counter" "$startepoch"
((counter++))
done
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment