1

How to make "dialog" progress bar increasing the counter, when each echo command is executed from function?

I have this code below as example, but I'm having problem of catching when command from function is executed. I can count number of "echo" in function and set as "items", but how to know when echo is finished and how to then increase the bar?

#!/bin/bash
function two() {
   echo "test2-1"; sleep 1;
   echo "test2-2"; sleep 1;
   echo "test2-3"; sleep 1;
   echo "test2-4"; sleep 1;
   echo "test2-5"; sleep 1;
}

(
    items=5
    processed=0
    while [ $processed -le $items ]; do
        pct=$(( $processed * 100 / $items ))
        echo "XXX"
        echo "Processing item $processed" # Here I wish instead $processed
                                          # to be value (test2-1, test2-2 etc.)
                                          # of processed echo
        echo "XXX"
        echo "$pct"
        processed=$((processed+1))
        sleep 3 # Instead of this it should be when echo is finished printing
    done
) | dialog --title "Gauge" --gauge "Wait please..." 10 60 0
5
  • How much do you know about the process, that produces the output? Do you know how many output lines to expect? Do you know what the output should be, when it is finished? Will the process exit when it has finished, but you don't really know when and how to expect it to finish?
    – sudodus
    Dec 4, 2021 at 15:00
  • Each process that produce the output can be any command. In this case executing ECHO. I know how many output lines are expected. Yes, I know what output should be after command is finished. Dec 4, 2021 at 16:03
  • OK, I'll try to make something useful.
    – sudodus
    Dec 4, 2021 at 16:12
  • Found this if it can help (unix.stackexchange.com/questions/77253/…) Dec 4, 2021 at 16:30
  • 5
    Does this answer your question? Bash console progress dialog with command output. If not, can you edit your post and state why not? Dec 4, 2021 at 16:51

1 Answer 1

2

General syntax for dialog --gauge

This demo script shows how to make dialog show progress.

#!/bin/bash

( echo 10;sleep 1;echo 50;sleep 1; echo 90;sleep 1;echo 100;sleep 1 ) | dialog --gauge 'text' 10 60 0
echo '##########'
i=0; while [ $i -le 100 ];do echo "$i";echo "#comment $i";i=$((i+10));sleep 1;done
echo '##########'
i=0; while [ $i -le 100 ];do echo "$i";echo "#comment $i";i=$((i+10));sleep 1;done| dialog --gauge 'text' 10 60 0
  • Output with numbers 0-100 is used.
  • Other output can be prefixed with # to be regarded as comments and it will not confuse dialog. It might even work with non-numbers (text lines) without prefix with #, but there may be surprises.

If you have other output, you should catch it, process it and pipe only the relevant values 0-100 to dialog.

Edit: known number of output lines

The following script assumes a known number of output lines (in this example 5) until it has finished, and uses the function actor to feed dialog:

#!/bin/bash

expected_outputs=5
ii=0

function two() {
   echo "test2-1"; sleep 1;
   echo "test2-2"; sleep 1;
   echo "test2-3"; sleep 1;
   echo "test2-4"; sleep 1;
   echo "test2-5"; sleep 1;
}

function actor () {
while read ans
do
    echo "# $ans"
    ii=$((ii+1))
    echo $((ii*100/expected_outputs))
done
}

# main

two | actor

two | actor | dialog --title "Gauge" --gauge "Wait please..." 10 60 0

Edit 2: zenity --progress

In a graphical desktop environment you can use the following matching command line with zenity:

two | actor | zenity --progress --title "Gauge" --text="string" --percentage=0 --auto-close --width=300

Not the answer you're looking for? Browse other questions tagged .