Have you ever wanted to write a bunch of words just in the same line over and over again?
Many applications, like encoders or converters, do that in order to show dynamic information to the user without filling up the screen (and thus the terminal buffer).
Here is how you can do it easily in bash.
The code is quite simple:
while :; do
echo -n -e "\r$(date)"
sleep 1
done
The trick consists of three parts:
With the same approach we can develop a fancy text progress bar.
For example, here is a nice cp command equipped with a simple progress bar:
#!/bin/bash
bar_width=30
tick=0.2
if [ $# -ne 2 ]; then
cat << EOM
$0: missing file operand
Use $0 filein fileout.
EOM
exit 1
fi
function draw_progressbar() {
# parameters:
# $1 - actual size
# $2 - final size
local part=$1
local place=$((part*bar_width/$2))
local i
echo -en "\r$((part*100/total))% ["
for i in $(seq 1 $bar_width); do
[ "$i" -le "$place" ] && echo -n "#" || echo -n " ";
done
echo -n "]"
}
total=$(stat -c "%s" $1)
cp $1 $2 &
sleep $tick;
while fuser -s $2; do
draw_progressbar $(stat -c "%s" $2) $total
sleep $tick;
done
draw_progressbar $(stat -c "%s" $2) $total
echo ""
References: