The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's manas6 UNIX for Dummies Questions & Answers 0 06-05-2008 03:44 AM
Useful piped filter combinations? Nelledawg UNIX for Dummies Questions & Answers 3 11-02-2007 01:28 PM
return code of a unix command ramky79 Shell Programming and Scripting 1 08-02-2007 12:34 PM
Mailx Return Email Command lisa0703 Shell Programming and Scripting 1 01-23-2007 05:38 PM
perl... how to tell if a piped command is still running? boytheo Shell Programming and Scripting 0 08-17-2005 08:01 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-21-2008
Registered User
 

Join Date: Sep 2006
Location: Mysore, India
Posts: 160
Return value of piped command?

Code:
grep $SEARCH_STRING /etc/passwd | cut -d":" -f 1,5
I need to check the $? value of grep in the above. If I place a test for $? after the above piped command, it returns success status of grep piped to cut.

How can I get the success status of grep alone?
Reply With Quote
Forum Sponsor
  #2  
Old 03-21-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,059
Different shells have different mechanisms for dealing with this situation.

Bash 3.0+ and ksh93 have the pipefail option (set -o pipefail) which changes the exit code behavior of pipelines and reports the exit code of the pipeline as the exit code of the last program to return a non-zero exit code.

Bash has the PIPESTATUS array variable which contains a list of exit status values from the processes in the most-recently-executed foreground pipeline

zsh has pipestatus.

Another way is to use coprocesses.
Reply With Quote
  #3  
Old 03-21-2008
Registered User
 

Join Date: Sep 2006
Location: Mysore, India
Posts: 160
I'm using ksh and the command set -o pipefail fails with the message
Code:
ksh: pipefail: bad option(s)
Can you please throw more light on coprocesses and the usage?
Reply With Quote
  #4  
Old 03-21-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,059
Quote:
I'm using ksh and the command set -o pipefail fails with the message .....
Then you are apparantly using either ksh88 or pdksh.
Reply With Quote
  #5  
Old 03-21-2008
Moderator
 

Join Date: Feb 2007
Posts: 2,295
You to combine exit statuses logically, so that you can test more than one thing at a time.

Code:
statement1 && statement2
means execute statement1 and if its exit status is 0 (sucessful) then execute statement2

Code:
statement1 || statement2
means execute statement1 and if its exit status is not 0 then execute statement2


Regards
Reply With Quote
  #6  
Old 03-23-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
But you can't do that in a pipeline.

Another makeshift solution would be to invoke a subshell and have it somehow smuggle out the exit status to a dedicated file descriptor. I don't think we want to go there, though.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 08:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0