How to get exit code in a pipe-lined command? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to get exit code in a pipe-lined command?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-09-2008
pankai pankai is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 5 February 2008, 12:57 PM EST
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
How to get exit code in a pipe-lined command?

I have a question about how to get the exit code of the first command when it appears in a pipe-lined command.
For example, I have the following script:

grep abc dddd | tee -a log
if [[ $? -ne 0 ]]
then
echo "ERROR!"
fi

In the above script, [[ $? -ne 0 ]] is supposed to test the exit code of "grep abc dddd". But since it is in a pipe line, the $? actually stores the exit code of "tee -a log". So, [[ $? -ne 0 ]] is always false.

How to address this problem?

Thanks.
Sponsored Links
    #2  
Old 01-09-2008
pankai pankai is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 5 February 2008, 12:57 PM EST
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by pankai View Post
I have a question about how to get the exit code of the first command when it appears in a pipe-lined command.
For example, I have the following script:

grep abc dddd | tee -a log
if [[ $? -ne 0 ]]
then
echo "ERROR!"
fi

In the above script, [[ $? -ne 0 ]] is supposed to test the exit code of "grep abc dddd". But since it is in a pipe line, the $? actually stores the exit code of "tee -a log". So, [[ $? -ne 0 ]] is always false.

How to address this problem?

Thanks.
I did some research myself. I turned out that we can get the status code for each command in a pipeline from $PIPESTATUS array.
Sponsored Links
    #3  
Old 01-10-2008
V3l0 V3l0 is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 10 April 2008, 10:18 AM EDT
Location: Belgium & France
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
res=$(grep abc dddd)

if [[ $? -ne 0 ]]
then
      echo "ERROR!"
else
      echo $res >> log
fi

    #4  
Old 01-10-2008
shamrock shamrock is offline Forum Advisor  
Registered User
 
Join Date: Oct 2007
Last Activity: 23 October 2014, 2:07 PM EDT
Location: USA
Posts: 1,488
Thanks: 20
Thanked 146 Times in 138 Posts
Quote:
Originally Posted by pankai View Post
I have a question about how to get the exit code of the first command when it appears in a pipe-lined command.
For example, I have the following script:

grep abc dddd | tee -a log
if [[ $? -ne 0 ]]
then
echo "ERROR!"
fi

In the above script, [[ $? -ne 0 ]] is supposed to test the exit code of "grep abc dddd". But since it is in a pipe line, the $? actually stores the exit code of "tee -a log". So, [[ $? -ne 0 ]] is always false.

How to address this problem?

Thanks.
You can check the size of the logfile before and after you grep for the string. This way if the size of the logfile remains the same then you know the grep failed.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Expect script: obtain the exit code of remote command Evan Shell Programming and Scripting 0 09-23-2010 05:46 AM
How to get the exit code of -exec in the find command oku Shell Programming and Scripting 8 09-29-2009 10:06 AM
exit status of command in a pipe line topcat8 UNIX for Dummies Questions & Answers 10 10-19-2007 04:39 AM
Where can I find a list of exit codes? (Exit code 64) jkuchar747 UNIX for Dummies Questions & Answers 3 12-07-2004 05:08 PM
Move command return with exit code of 2 handak9 UNIX for Advanced & Expert Users 1 08-26-2004 05:40 AM



All times are GMT -4. The time now is 03:54 PM.