exit status of command in a pipe line

Hi,

I am trying to test the exit status of the cleartool lsvtree statement below, but it doesn't seem to be working due to the tail pipe, which it is testing instead. Is there a way around this without adding a tonne of new code?

   cleartool lsvtree $testlocation/$exe_name | tail -15
   #exit out if not file not in dir
   if [ $? -ne 0 ]; then
          echo "Error: File not in test or application area"
          exit 1
   fi 

Thanks
Cath

added code tags for readability --oombera

unfortunetly not that i have found.

i also hit that wall when i do certin stuff.

This depends on the shell. ksh can smash through that wall by moving the "tail -15" to a co-process. I don't know that cleartool command so I'll use "cat /etc/passwd" for my example:

#! /usr/bin/ksh
exec 4>&1
tail -5 >&4 |&
exec >&p
cat /etc/passwd
exitcode=$?
exec >&- >&4
wait
echo exitcode = $exitcode
exit 0

something says i shouldnt have skipped that section on file handles.

As usual, very ingenious solution Perderabo.

Would there be any way possible to utilize xargs to facilitate this same requirement?

I use this technique as well however, on occassion, I'll use the following, if I know that I'll be parsing the output anyway:

set -A ARRAY $(
    somecommand
    print RC=$?
)

for i in ${ARRAY[@]}
do
     case $i in
        RC*) ... ;;
        whateverelse) ... ;;
     esac
done

Ok, I'm on a PC with no shell access at them moment, and it's not very elegant but this should also work.

(cat /etc/passwd 2> /dev/null || \
        echo "Error: File not in test or application area" ; \
        exit 1 )| tail -15;

This code is too elegant for me, please explain what it does and how?

bash and ksh93 have the pipefail option:

bash 3.2.25(17)$ ls x|tail
ls: cannot access x: No such file or directory
bash 3.2.25(17)$ echo $?
0
bash 3.2.25(17)$ set -o pipefail
bash 3.2.25(17)$ ls x|tail
ls: cannot access x: No such file or directory
bash 3.2.25(17)$ echo $?
2

bash and zsh have the PIPESTATUS/pipestatus array/variable:

bash 3.2.25(17)$ ls x|tail
ls: cannot access x: No such file or directory
bash 3.2.25(17)$ echo $PIPESTATUS
2
bash 3.2.25(17)$ ls x|tail
ls: cannot access x: No such file or directory
bash 3.2.25(17)$ echo ${PIPESTATUS[@]}
2 0
zsh 4.3.4% ls x|tail
ls: cannot access x: No such file or directory
zsh 4.3.4% echo $pipestatus
2 0

set -o pipefail doesnt seem to work on SunOS 5.8

bash-2.03$ uname
SunOS
bash-2.03$ set -o pipefail
bash: set: pipefail: unknown option name

echo $PIPESTATUS works on bash but not on ksh

bash-2.05$ ls -l rakesh | tail -5
rakesh: No such file or directory
bash-2.05$ echo $PIPESTATUS
2

Yes,
it's a new feature of bash Bash-3.0.

Hm ...., I said bash and zsh.
I said that the pipefail option is available in ksh93 (not ksh88)!