return code capturing for all commands connected by "|" ...


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers return code capturing for all commands connected by "|" ...
# 1  
Old 12-15-2008
Question return code capturing for all commands connected by "|" ...

Hi,

While I am using "|" to join multiple commands, I am not getting the return code when there is error in one of the commnads.

Eg:

b=`find /path/a*.out | xargs basename`

if [ $? != 0 ]; then
echo "Error"
fi

if there is error while finding the file or getting the basename, the $? is having zero value. But I require to know any error in that line of code.

Can any one tell me how to capture error in all the commands that are connected by "|" and make sure every one succeeded before proceeding further.

Thanks and Regards
# 2  
Old 12-15-2008
I'd "unpipe the piping", so to speak:

Code:
base=$( find someFile )
if [ $? -ne 0 ]
then
  echo "Error: $?"
  exit 1
else
  base=$( echo "$base" | xargs basename )
  if [ $? -ne 0 ]
  then
    echo "Error: $?"
    exit 1
  else
    echo "$base"
  fi
fi
exit 0

# 3  
Old 12-15-2008
Thank you dr.house for giving detail script.

So, can I take it as a thumb rule that its better not to use piping in the case we are not sure if the commands involved in piping are going to return true status?

Thanks and Regards
# 4  
Old 12-15-2008
from comp.unix.shell FAQ:
Code:
13. How do I get the exit code of cmd1 in cmd1|cmd2

    First, note that cmd1 exit code could be non-zero and still don't
    mean an error. This happens for instance in

    cmd | head -1

    you might observe a 141 (or 269 with ksh93) exit status of cmd1,
    but it's because cmd was interrupted by a SIGPIPE signal when
    "head -1" terminated after having read one line.

    To know the exit status of the elements of a pipeline
    cmd1 | cmd2 | cmd3

    a. with zsh:

       The exit codes are provided in the pipestatus special array.
       cmd1 exit code is in $pipestatus[1], cmd3 exit code in
       $pipestatus[3], so that $? is always the same as
       $pipestatus[-1].

    b. with bash:

       The exit codes are provided in the PIPESTATUS special array.
       cmd1 exit code is in ${PIPESTATUS[0]}, cmd3 exit code in
       ${PIPESTATUS[2]}, so that $? is always the same as
       ${PIPESTATUS: -1}.

    c. with any other Bourne like shells

       You need to use a trick to pass the exit codes to the main
       shell.  You can do it using a pipe(2). Instead of running
       "cmd1", you run "cmd1; echo $?" and make sure $? makes it way
       to the shell.

       exec 3>&1
       eval `
         # now, inside the `...`, fd4 goes to the pipe
         # whose other end is read and passed to eval;
         # fd1 is the normal standard output preserved
         # the line before with exec 3>&1
         exec 4>&1 >&3 3>&-
         {
           cmd1 4>&-; echo "ec1=$?;" >&4
         } | {
           cmd2 4>&-; echo "ec2=$?;" >&4
         } | cmd3
         echo "ec3=$?;" >&4
       `

    d. with a POSIX shell


       You can use this function to make it easier:

       run() {
         j=1
         while eval "\${pipestatus_$j+:} false"; do
           unset pipestatus_$j
           j=$(($j+1))
         done
         j=1 com= k=1 l=
         for a; do
           if [ "x$a" = 'x|' ]; then
             com="$com { $l "'3>&-
                         echo "pipestatus_'$j'=$?" >&3
                       } 4>&- |'
             j=$(($j+1)) l=
           else
             l="$l \"\$$k\""
           fi
           k=$(($k+1))
         done
         com="$com $l"' 3>&- >&4 4>&-
                    echo "pipestatus_'$j'=$?"'
         exec 4>&1
         eval "$(exec 3>&1; eval "$com")"
         exec 4>&-
         j=1
         while eval "\${pipestatus_$j+:} false"; do
           eval "[ \$pipestatus_$j -eq 0 ]" || return 1
           j=$(($j+1))
         done
         return 0
       }

       use it as:

       run cmd1 \| cmd2 \| cmd3
       exit codes are in $pipestatus_1, $pipestatus_2, $pipestatus_3

# 5  
Old 12-16-2008
Quote:
Originally Posted by new_learner
So, can I take it as a thumb rule that its better not to use piping in the case we are not sure if the commands involved in piping are going to return true status?
Admittedly, I tend to focus on the end of such a pipeline: if what drops out of there passes any checks for plausibility, I let the script proceed - otherwise, I force it to quit (returning the last script line processed as exit status) ... but that's only me Smilie
# 6  
Old 12-16-2008
Thank you for such a detailed explanation vgersh99. I really appreciate putting these details here. I dont know if I can ask this question, how do I know on which shell I am working? (I am new to Unix and triying to learn bits and pieces.) So, that I want to understand which of a,b,c,d options apply to me and later understand that option. I donot understand the code there mentioned in those options. But, I will do ground work of searching this site for those commands and try to understand what is mentioned there.

Thank you dr.house. I will keep your option in mind. But, the reason I want to make sure all the things pass correctly is because, the basename command used in pipe will hang for ever if it donot get any response from its previous command. I want to make sure what ever the error be I want to capture it and log exactly where the error was and exit the program. (I want to do this while I am learning because it took me a while to understand where the problem is and debug it. I want to save some time while I am learning Smilie )

Thank you guys...
# 7  
Old 12-16-2008
Quote:
Originally Posted by new_learner
But, the reason I want to make sure all the things pass correctly is because, the basename command used in pipe will hang for ever if it donot get any response from its previous command.
If this held true (which I dare to doubt - my Bash grumbles but fails to hang ...), in case of an error, you'd never reach the end of your little pipeline, let alone make use of the elegant solution proposed by vgersh99 ...?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract delta records using with "comm" and "sort" commands combination

Hi All, I have 2 pipe delimited files viz., file_old and file_new. I'm trying to compare these 2 files, and extract all the different rows between them into a new_file. comm -3 < sort file_old < sort file_new > new_file I am getting the below error: -ksh: sort: cannot open But if I do... (7 Replies)
Discussion started by: njny
7 Replies

2. Solaris

How to execute a ".profile" depending on the server from where the user is connected.

Hello, I have two Solaris servers on a cluster. I have created the same user "X"on the two nodes (same UID and same GID). The file ".profile" of this user "X" is located in a filesystem that is seen by the two nodes. Each server should have a ".profile" of this user different of the other... (6 Replies)
Discussion started by: adilyos
6 Replies

3. Red Hat

files having Script which works behind "who" & "w" commands

Dear All, plz print the path of files which have the script of "who" & "w" commands. thnx in advance. (6 Replies)
Discussion started by: saqlain.bashir
6 Replies

4. Solaris

Relation btw commands, "man" and "more" ???

Hi guys, Hope u r doing find. I have this query. When we check the manual pages for a certain command, say man cat we see the manual page with more What is UNIX really doing here, I mean why not less command instead of more command. And can we have UNIX display the manual pages with less command... (2 Replies)
Discussion started by: gabam
2 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Number of times a user is "connected"?

hi i wish make a shell to look for the number of times a user is connected then makes a "whatch dog" by crontab to handle this i could do it through ps-fedal| grep user_name | wc-l ... etc. but others suggested me another algorithm :mad: and thought to use w, or finger, or who but it have a... (1 Reply)
Discussion started by: edgar287
1 Replies

7. Shell Programming and Scripting

Unix commands delete all files starting with "X" except "X" itself. HELP!!!!?

im a new student in programming and im stuck on this question so please please HELP ME. thanks. the question is this: enter a command to delete all files that have filenames starting with labtest, except labtest itself (delete all files startign with 'labtest' followed by one or more... (2 Replies)
Discussion started by: soccerball
2 Replies

8. UNIX for Dummies Questions & Answers

Command 'rm -f -r "0yfOYy-0008Nq-2j-32233-K"' failed with return code 1 and error mes

I would like to know what means this error and how to fix it Command 'rm -f -r "0yfOYy-0008Nq-2j-32233-K"' failed with return code 1 and error message Thank you (3 Replies)
Discussion started by: linuxbee
3 Replies

9. Shell Programming and Scripting

Return code is "0" though the command fails.

The return code is "0" though the command fails. How to get a return code of "1" for this command when it fails or modify the command to get the right return code? On HP UNIX #-------- SCRIPT ---- #!/bin/ksh find /opt/oracle/oroem/product/10.2.0.4/rdbms/audit/ \( -name "*.aud" \) -mtime +1... (3 Replies)
Discussion started by: pbekal
3 Replies

10. UNIX for Advanced & Expert Users

Commands on Digital Unix equivalent to for "top" and "sar" on other Unix flavour

Hi, We have a DEC Alpha 4100 Server with OSF1 Digital Unix 4.0. Can any one tell me, if there are any commands on this Unix which are equivalent to "top" and "sar" on HP-UX or Sun Solaris ? I am particularly interested in knowing the CPU Load, what process is running on which CPU, etc. ... (1 Reply)
Discussion started by: sameerdes
1 Replies
Login or Register to Ask a Question