I'm writing an application that installs an upgrade using opkg packaging system.
Is there a way to get the overall progress so that I can make a progress bar?
I'm writing an application that installs an upgrade using opkg packaging system.
Is there a way to get the overall progress so that I can make a progress bar?
I am unfamiliar with opkg to know if it provides a native progress bar; the closest thing I see is the -V
verbosity level. If there's no better solution, then you could save the below script (to, say, a file named jspb
), make it executable (chmod u+x jspb
), then call jspb opkg upgrade packagename
to get a progress bar.
There are better ways to create progress bars, but I wanted to write something that would simulate a progress bar. It has one main requirement: the existence of the /proc pseudo-filesystem, so that it can track the progress of the job you give it. Otherwise, it is fairly simplistic and so should work in most shells (tested in bash, ksh, dash, and Bourne sh). Because it's so portable, shellcheck complains -- rightly so -- about the "antiquated" and "legacy" constructs that the script uses. It does manage to fill your screen's columns intelligently -- try resizing your window mid-update!
#!/bin/sh
# Jeff's progress bar
# *simulates* a progress bar
# better options:
# https://github.com/Xfennec/progress
# http://www.theiling.de/projects/bar.html
if [ ! -n "$*" ]
then
echo Nothing to do, exiting!
exit 0
fi
if [ ! -d /proc/$$ ]
then
echo Missing /proc, sorry
exit 1
fi
sh -c "$*" &
PID=$!
# give the command a chance to fail immediately, if it will
sleep 1
# dash, Bourne sh do not have $RANDOM
HAVERANDOM=0
if [ -n "$RANDOM" ]
then
HAVERANDOM=1
fi
i=1
while [ -d /proc/$PID ]
do
# simulate progress
if [ $HAVERANDOM ]
then
sleep `expr $RANDOM % 4`
else
sleep `expr $i % 4`
fi
# see if our window got resized
cols=`tput cols`
if [ $i -ge $cols ]
then
printf "\r"
# clear out the old progress bar and start over!
while [ $i -gt 1 ]
do
printf " "
i=`expr $i - 1`
done
printf "\r"
fi
# making progress!
printf "#"
i=`expr $i + 1`
done
# only clear the progress bar it if we wrote something
if [ $i -gt 1 ]
then
pacman=1
printf "\r"
while [ $pacman -lt $i ]
do
printf " "
pacman=`expr $pacman + 1`
done
printf "\r"
fi
Master copy is on Github
jspb sleep 10000
in a narrow-ish terminal window and see... it wraps around and starts over. – Jeff Schaller Apr 15 '16 at 17:05