Return values in pipelines

Randal Schwartz merlyn at iwarp.intel.com
Fri Aug 24 07:56:12 AEST 1990


In article <7372 at star.cs.vu.nl>, maart at cs (Maarten Litmaath) writes:
| In article <1990Aug22.211057.19850 at agate.berkeley.edu>,
| )	      How can I detect if something went wrong on the previous
| )commands in the pipeline (prog2, prog3)?
| 
| You can do something like this:
[program using fd 3 and /bin/sh fd duping deleted]

Hmm.  Similar to what I was going to do, except that I would have used
temp files (remember... a temp file is just a pipe with an attitude
and a strong will to live).

#!/bin/sh
trap "rm /tmp/r$$.*; exit" 0 1 2 3 15

rm -f /tmp/r$$.?
(A || echo A exited with $? >/tmp/r$$.A) |
(B || echo B exited with $? >/tmp/r$$.B) |
(C || echo C exited with $? >/tmp/r$$.C) |
(D || echo D exited with $? >/tmp/r$$.D)

case /tmp/r$$.? in
	/tmp/r$$.\?)
		exit 0;;
	*)
		cat /tmp/r$$.?; exit 1;;
esac

With temp files, you won't get a race condition if both processes
decide to exit at *exactly* the same time (rare, but it'll happen!).

Just another /bin/sh hacker,
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn at iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/



More information about the Comp.unix.questions mailing list