LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-30-2012, 05:45 PM   #1
Garrett85
Member
 
Registered: Jan 2011
Posts: 88

Rep: Reputation: 0
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.
 
Old 10-30-2012, 09:14 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 1,603

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Quote:
Originally Posted by Garrett85 View Post
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.

Last edited by rknichols; 10-30-2012 at 09:18 PM.
 
1 members found this post helpful.
Old 10-31-2012, 12:26 AM   #3
Garrett85
Member
 
Registered: Jan 2011
Posts: 88

Original Poster
Rep: Reputation: 0
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 View Post
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.
 
Old 10-31-2012, 11:16 AM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 1,603

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
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.

Last edited by rknichols; 10-31-2012 at 11:24 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash Shell - PIPESTATUS mrm5102 Linux - Newbie 3 04-07-2011 09:40 AM
Need help understanding this PostCode Linux - Newbie 3 11-09-2007 05:12 PM
help understanding ln dr_zayus69 Linux - General 3 01-14-2005 09:33 PM
A better understanding! nny0000 Slackware 1 04-14-2004 01:01 PM
Understanding X?? nny0000 Linux - General 2 02-25-2004 03:07 AM


All times are GMT -5. The time now is 07:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration