I have made a pretty basic progress bar in BASH like this :-
doned=$1 #amount completed
total=$2 #total amount
doned=`echo $doned $total | awk '{print ($1/$2)}'`
total=`tput cols | awk '{print $1-10}'`
doned=`echo $doned $total | awk '{print int(($1*$2))}'`
echo -n $doned"% [ "
for i in $(seq 1 $doned); do
echo -n "="
done
for i in $(seq $((doned+1)) $total); do
echo -n "-"
done
echo " ]"
This works exactly like I want it to.
This script runs within a loop in another script. I want it to always display at the bottom of the terminal, or any other fixed place.
The loop is somewhat like this:-
for i in 10 20 30 40; do
echo -n "Do you want to continue on to the next step? (y/n): ";
read $yn
if [[ "$yn" == "n" ]] || [[ "$yn" == "N" ]]; then
exit 1; # stop the script
d_3=0;
fi
doned=`some command` ### Get number of completed files
totalss=`some other command` ### Get total number of files
bash ./libraries/prog.sh $doned $totalss
done
So what I want is that the progress bar remain at the bottom even when inputting values, or displaying something. Is there a way to do this, preferably without having to install anything extra? Most of the computers I want to use this script on are either Debian version 8+ or Ubuntu 16+ systems
Best Answer
Maybe, but I think it's going to be harder than you expect.
You can make the cursor move around the terminal by outputting ANSI control codes. For more on them, see here.
In principle, you could move the cursor to your progress bar, add an
=
, and then move it back to wherever you plan to print output. Then on the next=
you'd have to do it again... but this would probably be ugly.If you're willing to depart from bash, you can probably find libraries that make this kind of thing easier (like this).