LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   understanding $PIPESTATUS (https://www.linuxquestions.org/questions/programming-9/understanding-%24pipestatus-4175434812/)

Garrett85 10-30-2012 04:45 PM

understanding $PIPESTATUS
 
Code:

( make 2>&1 | tee make.log && exit $PIPESTATUS ) &&
Could someone give me a detailed explanation of the above line? I know that tee is creating a text file while also allawing the output to come to the screen but need a better understanding of $PIPESTATUS. Is "exit" exiting everything to the left of it or $PIPESTATUS to the right? Why are there two sets of "&&"? And I'M sure the variable $? comes it to play somewhere here. Thanks.

rknichols 10-30-2012 08:14 PM

Quote:

Originally Posted by Garrett85 (Post 4818493)
Code:

( make 2>&1 | tee make.log && exit $PIPESTATUS ) &&

Your confusion is understandable since the relative simplicity of the syntax is actually hiding a fair amount of complexity.

PIPESTATUS is an array that contains the exit codes of each command in the preceding pipeline. In this case, PIPESTATUS[0] contains the exit code from make, and PIPESTATUS[1] contains the exit code from tee. Writing "$PIPESTATUS" (with no subscript) is a shorthand for ${PIPESTATUS[0]}, the exit code from make.

The "&&" is the boolean AND operator, which causes the command or pipeline that follows to be executed only if the preceding command or pipeline returned a "success" exit code (0). The exit code from a pipeline is normally the exit code from the last command in that pipeline.

So, let's look at that whole parenthesized subprocess. If the tee command fails (presumably because the specified file could not be written), then the "&&" operator prevents the "exit $PIPESTATUS" command from being executed, and the subprocess will exit with the exit code from the pipeline, i.e., that returned by tee. If the tee command succeeds, then the "exit $PIPESTATUS" will be executed, causing the subprocess to exit with whatever status was returned by make. The net result is that the subprocess will return a non-zero exit status if either the make or tee command fails. Note that this fairly simple case is just doing what the shell's "pipefail" option would do (make a pipeline return a failure if any command in the pipeline fails), but is perhaps a bit more portable.

The final "&&" operator is not really part of this subprocess, but would prevent the next command in the script from executing if this subprocess returns a non-zero exit code.

Garrett85 10-30-2012 11:26 PM

Can it always be assumed that a good exit status or code will be 0? Or is that just convention? What happens if the exit status is not 0. I am also sometimes getting the stderror error output from 2>&1 mixed up with the exit status NOT 0.

Quote:

Originally Posted by rknichols (Post 4818600)
Your confusion is understandable since the relative simplicity of the syntax is actually hiding a fair amount of complexity.

PIPESTATUS is an array that contains the exit codes of each command in the preceding pipeline. In this case, PIPESTATUS[0] contains the exit code from make, and PIPESTATUS[1] contains the exit code from tee. Writing "$PIPESTATUS" (with no subscript) is a shorthand for ${PIPESTATUS[0]}, the exit code from make.

The "&&" is the boolean AND operator, which causes the command or pipeline that follows to be executed only if the preceding command or pipeline returned a "success" exit code (0). The exit code from a pipeline is normally the exit code from the last command in that pipeline.

So, let's look at that whole parenthesized subprocess. If the tee command fails (presumably because the specified file could not be written), then the "&&" operator prevents the "exit $PIPESTATUS" command from being executed, and the subprocess will exit with the exit code from the pipeline, i.e., that returned by tee. If the tee command succeeds, then the "exit $PIPESTATUS" will be executed, causing the subprocess to exit with whatever status was returned by make. The net result is that the subprocess will return a non-zero exit status if either the make or tee command fails. Note that this fairly simple case is just doing what the shell's "pipefail" option would do (make a pipeline return a failure if any command in the pipeline fails), but is perhaps a bit more portable.

The final "&&" operator is not really part of this subprocess, but would prevent the next command in the script from executing if this subprocess returns a non-zero exit code.


rknichols 10-31-2012 10:16 AM

The "success" exit code is 0 by definition**. Nothing special happens when the exit code is non-zero, but that exit code can be tested by the calling program or script in order to decide how to proceed.

Whether or not stderr is redirected has nothing whatsoever to do with the exit code. It is quite common for programs to send informational messages (i.e., not indicating an error) to stderr.

** It has been said that the fall of the Roman Empire was due to their numbering system lacking a zero, thus giving their C programs no way to indicate success. :rolleyes:


All times are GMT -5. The time now is 06:43 PM.