Detecting exit status if piped processes HELP!

Detecting exit status if piped processes HELP!

Post by Bitter Spoc » Fri, 10 Aug 2001 11:19:47



I have a script which will build some source code by filtering it first
through a set of
preprocessors first.

for example: if I had a CSH script:

#! /bin/csh -f

foreach file ($argv[1-])
    if(-e $file) then
        cat $file |first_pp|second_pp|third_pp|fourth_pp >$file.dat
        if($status) then
            echo compilation failed
            exit 1
        endif
    endif
end

The problem is that the $status only reflects the exit status of
"fourth_pp".
I need to exit with errors if "first_pp", or "second_pp", or "third_pp" also
exited abnormally.

I could do each process separately and make intermediate files, but the file
sizes are too large in-between, and that seems like a kludge. Is there an
elegant way ?

My only other way is to have each preprocessor create an error file, and
detect its existence at the end. That also seems like a kludge.

Is there a way to do this? another Shell type that support this.

#! /bin/csh -ef  does not do this either.

HELP!

please email:

 
 
 

Detecting exit status if piped processes HELP!

Post by Andrew Giert » Fri, 10 Aug 2001 12:04:10


 Bitter> I have a script which will build some source code by
 Bitter> filtering it first through a set of preprocessors first.

 Bitter> for example: if I had a CSH script:

that's your first mistake. Don't ever write scripts in csh.

The problem is solveable in Bourne/POSIX type shells, though the
contortions required are somewhat painful. Using bash it is trivial
thanks to the PIPESTATUS array variable.

In a POSIX type shell one solution is along these lines:

results=$( ( ( prog_1 <$infile || echo "prog_1 failed: $?" >&3)
             | (prog_2 || echo "prog_2 failed: $?" >&3)
             | (prog_3 || echo "prog_3 failed: $?" >&3) >$outfile
           ) 3>&1 )
if [ -n "$results" ]
then
    # something failed
    echo "processing failed:"
    echo "$results"
    exit 1
fi

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>

 
 
 

Detecting exit status if piped processes HELP!

Post by Chris Tore » Fri, 10 Aug 2001 19:21:48




Quote:>The problem is solveable in Bourne/POSIX type shells, though the
>contortions required are somewhat painful. Using bash it is trivial
>thanks to the PIPESTATUS array variable.

>In a POSIX type shell one solution is along these lines:

>results=$( ( ( prog_1 <$infile || echo "prog_1 failed: $?" >&3)
>             | (prog_2 || echo "prog_2 failed: $?" >&3)
>             | (prog_3 || echo "prog_3 failed: $?" >&3) >$outfile
>           ) 3>&1 )
>if [ -n "$results" ]
>then
>    # something failed
>    echo "processing failed:"
>    echo "$results"
>    exit 1
>fi

Even in the ancient original Bourne shell it is possible, by
doing something like this:

  STAT1=/tmp/stat.$$.1
  STAT2=/tmp/stat.$$.2
  STAT3=/tmp/stat.$$.3

  rm -f $STAT1 $STAT2 $STAT3
  trap "rm -f $STAT1 $STAT2 $STAT3" 0 1 2 3 15

  (prog1; echo $? > $STAT1) < infile |
  (prog2; echo $? > $STAT2) |
  (prog3; echo $? > $STAT3) > $outfile

  # each status is now available in the corresponding file

A similar trick might work in csh, but programming is a lot easier
in virtually any other shell. :-)
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)


 
 
 

Detecting exit status if piped processes HELP!

Post by d.. » Tue, 21 Aug 2001 19:15:46



>I have a script which will build some source code by filtering it first
>through a set of
>preprocessors first.

>for example: if I had a CSH script:

>The problem is that the $status only reflects the exit status of
>"fourth_pp".
>I need to exit with errors if "first_pp", or "second_pp", or "third_pp" also
>exited abnormally.

>Is there a way to do this? another Shell type that support this.

This exact issue is addressed in the informative rant "csh programming
considered harmful" by Tom Christiansen. One of many URLs where this is
available is http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/

The solution (although awkward) is possible in sh but impossible in csh.
The solution is to do this, given A|B, where you want the exit status of A.

((A 2>&1 1>&3 3>&- 4>&- ; echo $? >&4)| B 1>&2 3>&- 4>&- ) 4>&1

Cheers,
Dave.

 
 
 

1. Detecting Exit Statuses From Background Processes

Let's say I have a script which kicks off an unknown number of
processes which I want to run in the background. For example,

for anId in ${ids[*]}
do
   runProgram $anId &
done
wait

where "runProgram" is another script to which I have to pass an id. Of
course, every instance of "runProgram" gets a different id and
because "runProgram" can take a while I want each to process in the
background.

However, I want to keep track of the exit status of each instance
of "runProgram" and then exit this script ( the one which
launched "runProgram" ) with the highest status from all the
runPrograms.

I don't think I can capture the exit status directly via $? so I
decided to "echo" the exit status from runProgram before runProgram
terminates. For example, the last line of runProgram is:

echo $status

where $status is of course some number which indicates whether the
script completed successfully or not.

If I try to capture this return value of runProgram into a variable
then the background indicator ( & ) gets ignored and all the runProgram
commands run serially. For example, here I'm trying to capture the
result of runProgram into an entry in an array of return codes. Each
runProgram's return value would be stored into a different entry in the
array.

returnCodes[$1]=`runProgram $anId &`

But, as stated, in the above case the process does not run in the
background.

If there anyway to do this?

Sent via Deja.com http://www.deja.com/
Before you buy.

2. license manager for commerce

3. Pipes and detecting exit code of child process

4. How to install Netgear FA310TX

5. exit status of the command in pipe from shell & named pipe

6. virtual memory exhausted

7. Difficulty retrieving exit status of command before pipe

8. Handy dandy port reference

9. Get exit status of first command in pipe.

10. Preserving exit status in pipes

11. Bash shell is always returning last exit status of the pipe

12. Getting Exit Status from subshell and pipe

13. exit status from inside of pipe?