How to Pass Data Outside Process for Zenity Progress

exitprocessshellzenity

Usually this would be a question about how to pass data from a subprocess to a main process, but maybe zenity has some extra quirks so please focus on zenity.

Example:

#!/bin/sh
(
echo "10" ; sleep 1
echo "# Updating mail logs" ; sleep 1
echo "20" ; sleep 1
echo "# Resetting cron jobs" ; sleep 1
echo "50" ; sleep 1
echo "This line will just be ignored" ; sleep 1
echo "75" ; sleep 1
echo "# Rebooting system" ; sleep 1
echo "100" ; sleep 1
) |
zenity --progress \
  --title="Update System Logs" \
  --text="Scanning mail logs..." \
  --percentage=0

# ... here main process continues (x)

Each step in practice can fail (there is more than just an echo — tar, computing md5, zipping, unzipping, you name it), and I would like to set, for example, variable ERR to some message and quit the progress subprocess, and after that display content of ERR and quit for good.

The problem is, ERR is local variable, so I can break the subprocess, but nevertheless I cannot pass ERR outside. I cannot also display it locally, because the main script will continue anyway, unaware that the subprocess failed.

So the question is, how can I pass an error code, message, anything to the main process (continuing at the (x) point)?

Process substitution:

#!/bin/sh
zenity --progress \
  --title="Update System Logs" \
  --text="Scanning mail logs..." \
  --percentage=0 < <(
echo "10" ; sleep 1
echo "# Updating mail logs" ; sleep 1
...

This didn't work for me too, i.e. ERR was not visible to the main script.

Best Answer

What you probably is want is PIPESTATUS (from man bash:)

An array variable (see Arrays below) containing a list of exit status 
values from the processes in the most-recently-executed foreground pipeline 
(which may contain only a single command).
Related Question