2

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?

techraf
  • 5,941

1 Answers1

0

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

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • Can you explain how the simulated progress is generated? For example, what's the strategy of incrementing the progress? – user3528438 Apr 15 '16 at 17:01
  • It's a simulation; an artificial delay until the actual process completes. You might have seen these sorts of "progress" bars in other operating systems. :) – Jeff Schaller Apr 15 '16 at 17:02
  • Yes, but how do you solve the problem that the progress bar will stay at 90% for a while? Say the process takes 10 minutes but the progress bar reaches the end long before that? – user3528438 Apr 15 '16 at 17:04
  • try 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
  • I don't like (anonymous) downvotes (which wasn't mine) ... so undo. – Pierre.Vriens Feb 08 '18 at 11:17