YAD Simple Progress Bar
Needed a progress bar for a bash script and found that YAD had an option for it. This code found on the web was really helpful. It uses a for loop and pipes the loop count into YAD to increment the bar and also show the count number, when run you get this:
Code: Select all
#!/bin/bash
for ((i=1; i<=100; i++)) {
echo $i
echo "# $((i))%"
sleep 0.5
} | yad --progress \
--text="Progress demo..." \
--width=300 \
--button=yad-cancel \
--auto-kill
Notice that the bar also shows "21%", which is a bit deceiving since the only thing it is measuring is the loop count and the percentage of the bar that is completed. The maximum "count" for the bar is 100 and this appears to be hard coded in YAD. When the count reaches 100 the bar is complete (stops).
To make the bar more meaningful, changes these:
-i<=300 (increase upper loop limit to run longer)
-echo "# $((i))" (remove % from count display)
-sleep 1 (makes each loop 1 second)
When run you get this:
Again, the bar will stop at 100, but the count display will continue to increase. With sleep 1 the count is now time in seconds and represents elapsed time. Much more meaningful.
Now with a little more code we can make the bar loop while the count increases, this gives the user a continually moving bar. This is visually better than example 2.
Note in this code example the loop upper limit (-i<=500) really has no effect as long as it is greater than 100.
The last part of this code is how to apply the bar to a running process. Just put your process code in the "#do commands" section (replace the sleep 30 line).
Thanks to @rcrsn51 for help in determining the best way to end the bar code.
Code: Select all
#! /bin/bash
#250210
#pseudo progress, actually just a timer
#author: wizard on the Puppy Linux Forum
#this loops the bar from 1-100 until killed
#start counting and progress bar
for ((i=1; i<=500; i++)) {
if [[ $i = 100 ]];then
i2=$i3
i=1
fi
i3=$(($i2 + $i))
echo $i
echo "# $((i3))" #this will show number in the bar
sleep .1 #change this to 1 to show seconds
} | yad --title="Wait" \
--progress \
--text="Extracting...time" \
--width=400 \
--no-buttons &
PID=$!
#do commands or process, progress bar will run until these complete
sleep 30 #this can be any process or set of commands
#end the progress bar and close yad dialog
sleep 2
kill $PID
wizard