Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

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).
share|improve this answer
    
Thank you, for my program this will do the trick. –  greenoldman May 16 '12 at 20:53
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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