nALFS checking command result status of pipelines

Kevin P. Fleming kpfleming at linuxfromscratch.org
Sun Nov 2 12:51:22 PST 2003


Neven brought up a good point with the decompress/unpack command changes 
I made this week. The point is that:

zcat FOO | tar xv

returns 0 if everything works, and non-zero if any of the commands fail.

However:

zcatX foo | tar xv

returns 0, even though zcatX does not exist.

I believe I have found a way to check for this: bash (and zsh and ksh, 
at least) have an environment variable called PIPESTATUS that holds the 
result status from each command in the most-recently-executed pipeline. 
I believe PIPESTATUS is even in the relevant POSIX standard. Checking 
the contents of that array would let us know when this occurs, because 
${PIPESTATUS[0]} is 127 if the first command in the pipeline could not 
be executed at all.

The big problem here is how to get that information back from the 
subshell to nALFS. A construction like this works, although I can only 
confirm that the arithmetic expansion is POSIX- and SUS-compliant:

zcat FOO | tar xv && exit ${{PIPESTATUS[0]+PIPESTATUS{1]))

This causes the exit value of the subshell to be combined results of the 
two commands in the pipeline.

The only other way I can see to deal with this problem is to not use 
pipelines at all, but instead to issue each command individually and either:

- force the intermediate output into temporary files

or

- implement the pipes in nALFS itself.

Opinions/thoughts/flames/etc.?





More information about the alfs-discuss mailing list