Created
February 26, 2014 14:37
-
-
Save stevenrombauts/9230513 to your computer and use it in GitHub Desktop.
Bash Progress Bar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Slick Progress Bar | |
# Created by: Ian Brown ([email protected]) | |
# 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 | |
PUT 5 $halfDone; echo -e "\033[7m \033[0m" #Draw the bar | |
tput sgr0 | |
} | |
# Start Script | |
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
Thanks! This bar is very nice!
If you're interested, i've just made a little change to always print the whole bar and avoid skipping some parts of the bar if the progress is not regularly incremented.
https://gist.github.com/GustavoHoyer/6a361b732831ab0d7523/revisions