0

I may be very bad at implementing the progress bars mentioned here: How to add a progress bar to a shell script?. But I would very much like a progress bar for a shell install script I am making. It could (in terms of looks) be similar to this,

'[                           (00%)]/'
'[#####                      (33%)]\'
'[##################         (66%)]-'
'[##########################(100%)]|'

where we have a progress bar, a percentage counter and a spinning device. I would very much like for it to be modular so that I can use in other projects too but perhaps I then ask for something which is too complicated. I do not know how and what it should count, but I thought that it would be somewhat smart to simply count the number of lines executed in my script and compare this number to the total number of lines, but I am of course open for other suggestions.

Community
  • 1
  • 1
mstaal
  • 590
  • 9
  • 25
  • Lines take a varying amount of time to execute. It should be based on the expected time left. – Tim Jul 12 '16 at 21:52
  • I know. But right now I would just like some sort of script. If you know how to make an estimate of how much time is left I would very much enjoy seeing such a solution! :-) My problem is, however, that the script I am doing right know does a lot of apt-gets, downloads from GitHub and installing various .deb files, so I do not know how I should have a clue myself. – mstaal Jul 12 '16 at 22:29

2 Answers2

1

Here is an example of a progress bar, % counter & spinner :

#!/bin/bash

spin() {
    sp='/-\|'
    printf ' '
    while true; do
        printf '\b%.1s' "$sp"
        sp=${sp#?}${sp%???}
        sleep 0.05
    done
}

progressbar()
{
    bar="##################################################"
    barlength=${#bar}
    n=$(($1*barlength/100))
    printf "\r[%-${barlength}s (%d%%)] " "${bar:0:n}" "$1" 
}

spin &
pid=$!

#your task here

for i in `seq 1 100`;
do
    progressbar $i
    sleep 0.1
done

# kill the spinner task
kill $pid > /dev/null 2>&1

Spinner is started at the beginning and stopped at the end, the progress bar function is called each time % change.

You can customize progress bar style updating "\r[%-${barlength}s (%d%%)] " format

Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
  • Thank you very much for your help! Sadly, I cannot get it to work, though. I am trying to make the following install.sh file have a progress bar. :-/ https://github.com/mstaal/i3buntu – mstaal Jul 13 '16 at 11:53
  • The for loop is an example. Call progress bar function with specified % value each time you want to change the %. The moment when you want to change the % is up to your actions to be processed in your script. – Bertrand Martel Jul 13 '16 at 12:03
  • I now think I understand your point. But I get a syntax error when loading the script. It says that (for some reason) there should not be a left parenthesis when loading the spin function. – mstaal Jul 13 '16 at 17:08
  • I have removed `function` statement, it should be working fine with this – Bertrand Martel Jul 13 '16 at 17:49
0

You can check out this time based progress bar

It is quite smooth because it uses partial block characters

enter image description here

nachoparker
  • 1,678
  • 18
  • 14