Ideas and help for working with the progressbar CocoaDialog

In bash (and probably other shells) >(command) runs ‘command’ (in a sub process) and replaces the statement with a file handle (named pipe), so by using that (called process substitution) we can make the shell do the bookkeeping, which was manual in the example at the page (i.e. the shell will create the named pipe, and close it, when our command closes its output, which happens when the command completes).

So for the percantage, we can settle with:

for (( i = 1; i <= 100; i++ )); do
    echo "$i We're now at $i%"; sleep .05
done > >(CocoaDialog progressbar --title "My Program")

or

for (( i = 1; i <= 100; i++ )); do echo "$i $i% Complete..." ; sleep .05 ; done > >(./CocoaDialog progressbar --title "My Program")

If we need to run several commands, we can use a block like this:

{   command1
    command2
    command3
} > >(CocoaDialog progressbar --indeterminate --title "My Program")

And if we need a result back from the block of commands, we can redirect stderr instead of stdout, example:

res=$({
    sleep 5
    echo "That's all folks!"
} 2> >;(CocoaDialog progressbar --indeterminate --title "My Program"))
echo "Got back: $res"