Bash / Zenity — Percentage based progress in bash with a zenity output
A recent project i was looking at (not the ZCP one) required me to figure out how to do percentage based updates to zenity
I have boiled it down to a “one liner” code example
x=0;y=14;z=0; while [[ z -lt 100 ]]; do z=$(( $x*100/$y )); x=$(( x+1 ));echo $z 1>&2; echo $z; sleep 1s ; done |time zenity --progress --auto-close
This example will go through its loop incrementing by 1 each iteration. It will take the percentage calculated and output it both on your terminal, and in a zenity progress bar.
x=0;y=100;z=0; while [[ z -lt 100 ]]; do z=$(( $x*100/$y )); x=$(( x+1 ));echo $z 1>&2; echo $z;sleep 0.125s; done | zenity --progress --auto-close
In this example the program will increment by one from 0-100 with a quarter second delay between each increment and again show the updates both on the command line and via a Zenity progress bar, This lets you see the progress bar go through all of its update points.
x=99;y=100;z=1; while [[ z -gt 0 ]]; do z=$(( $x*100/$y )); x=$(( x-1 ));echo $z 1>&2; echo $z;sleep 0.125s; done | zenity --progress
This example is almost the same as the last EXCEPT it counts down from 99-0 in increments of 1
Note if Zenity –progress receives a value of 100 it locks up which is why we count down from 99
In these Examples
X= Part of the whole
Y= The whole
Z= The Percentage of the whole = the Part
For more in depth information on the mathmatics of percentages Take a look at the site where I obtained the above Percentage Formula image from : http://www.basic-mathematics.com/formula-for-percentage.html