Something that I'd like to share with you!

Saturday, August 21, 2021

Bash - progress bar

No comments :



Example on using bash loop to draw an ASCII progress bar with BASH.

The dispProgBar function takes 2 arguments, current progress and total progress number. In this example it starts with 1 until 10000. 

dispProgBar will then calculate percentage of progress and current bar length.

1st loop will draw current progress bar with a dark block (ASCII 219).

2nd loop will draw remaining progress bar with a light graphic character (ASCII 176).

At the end of the progress bar it will send "\r" to go back to the beginning of the line without printing a newline.
#!/bin/bash

function dispProgBar(){
	curProg=$1
	totalProg=$2
	barFullLen=50
	((perc = $curProg * 100 / $totalProg))
	((barLen = $curProg * $barFullLen / $totalProg))
	printf "%3s%% [" $perc

	for (( c=1; c<=$barLen; c++ )); do
		echo -ne "█"
	done

	for (( c=$barLen; c<$barFullLen; c++ )); do
		echo -ne "░"
	done

	echo -en "]\r"

	if [ $curProg -ge $totalProg ]; then
		echo ""
	fi
}

for i in {1..10000}
do
	dispProgBar $i 10000
done

No comments :