The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
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 06:08 PM
Move command return with exit code of 2 handak9 UNIX for Advanced & Expert Users 1 08-26-2004 05:40 AM
Exit Code in HP-UX KSH. mbb High Level Programming 3 03-15-2002 09:44 AM
All about exit code cdin2 Shell Programming and Scripting 2 03-11-2002 10:03 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-09-2008
pankai pankai is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 14
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.
  #2 (permalink)  
Old 01-09-2008
pankai pankai is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 14
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.
  #3 (permalink)  
Old 01-10-2008
V3l0 V3l0 is offline
Registered User
  
 

Join Date: Nov 2007
Location: Belgium & France
Posts: 70
Code:
res=$(grep abc dddd)

if [[ $? -ne 0 ]]
then
      echo "ERROR!"
else
      echo $res >> log
fi
  #4 (permalink)  
Old 01-10-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 741
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

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 10:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0