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.


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..." \

# ... 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:

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.


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).

I solved the problem as Fabian suggested (thank you again). However, I was not happy with the entire construction of the data flow. After all, it is Zenity which should monitor the progress of the work; I should not have to bend my entire program just to make it happy.

So, I changed the entire workflow. First I create a named pipe in order to communicate the progress to Zenity, and then Zenity works as it should (as a monitor). So I call:

(tail -f my_named_pipe) | zenity .... &

and when I want to set a label or progress I simply call

echo "# We are cruising..." > my_named_pipe

If I am done with progress I send "100" to Zenity. This allows my main script to work in a linear manner, without worrying about passing data from a subprocess, because there is no subprocess now.

  • But when you spawn the tail process with & doesn't it run until shutdown? – WinEunuuchs2Unix Jun 12 at 23:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.