Skip to content

Instantly share code, notes, and snippets.

@gohoyer
Forked from stevenrombauts/progressbar.sh
Last active November 6, 2017 10:54
Show Gist options
  • Save gohoyer/6a361b732831ab0d7523 to your computer and use it in GitHub Desktop.
Save gohoyer/6a361b732831ab0d7523 to your computer and use it in GitHub Desktop.
Bash Progress Bar
#!/bin/bash
# Slick Progress Bar
# Created by: Ian Brown (ijbrown@hotmail.com)
# Please share with me your modifications
# Note: From http://stackoverflow.com/questions/11592583/bash-progress-bar
# Functions
PUT(){ echo -en "\033[${1};${2}H";}
DRAW(){ echo -en "\033%";echo -en "\033(0";}
WRITE(){ echo -en "\033(B";}
HIDECURSOR(){ echo -en "\033[?25l";}
NORM(){ echo -en "\033[?12l\033[?25h";}
function showBar {
percDone=$(echo 'scale=2;'$1/$2*100 | bc)
halfDone=$(echo $percDone/2 | bc) #I prefer a half sized bar graph
barLen=$(echo ${percDone%'.00'})
halfDone=`expr $halfDone + 6`
tput bold
PUT 7 28; printf "%4.4s " $barLen% #Print the percentage
for (( i=6 ; i<=$halfDone ; i++ ))
do
PUT 5 $i; echo -e "\033[7m \033[0m" #Draw the bar
done
PUT 8 1; echo -e "Elapsed time: $(($currenttime-$starttime)) seconds"
PUT 9 1; echo -e "Total: $2"
PUT 10 1; echo -e "Done: $1"
tput sgr0
}
# Start Script
starttime=$(date +%s);
currenttime=$(date +%s);
clear
HIDECURSOR
echo -e ""
echo -e ""
DRAW #magic starts here - must use caps in draw mode
echo -e " PLEASE WAIT WHILE SCRIPT IS IN PROGRESS"
echo -e " lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk"
echo -e " x x"
echo -e " mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj"
WRITE
#
# Insert your script here
for (( i=0; i<=50; i++ ))
do
showBar $i 50 #Call bar drawing function "showBar"
sleep .2
done
# End of your script
# Clean up at end of script
PUT 10 12
echo -e ""
NORM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment