Want to get the exit status

Hi All,
I am trying to create a zip file with all the txt files(these are in large number) in the current directory. I am able to do this operation sucessfully.

After this i want to get the status of the tar command executed and do accordingly. When i am trying with the below code, the status returned is for the entire operation i.e for find + tar. But what i want is only the status of the tar command.

can anyone help me with this.

#!/bin/sh

find . -name '*.txt' -print | tar -czf save1.tgz --files-from - > /dev/null 2>&1

RC=$?
if [ $RC -gt 0 ] ; then
   echo " tar operation failed with $RC "
else
   echo " tar operation passed with $RC "
fi

What made you to think that exit status is for entire operation and not for the last run command?

just try running any such combination of commands and check $? you will get your answer.

I can show you an example

My bad, actually question is i am getting a incorrect return status from the entire command.
lets change the above find+tar to

find ./abc -name '*.txt' -print | tar -czf save1.tgz --files-from - > /dev/null 2>&1

RC=$?
if [ $RC -gt 0 ] ; then
   echo " tar operation failed with $RC "
else
   echo " tar operation passed with $RC "
fi
paddu:/tmp/pradeep # ls abc
/bin/ls: prg: No such file or directory
paddu:/tmp/pradeep # ./a.sh
find: ./abc: No such file or directory
 tar operation passed with 0 

Here the return status is 0 even the directory required doesnot exists.
How to get the correct status here

exit status contains the status of last command run.. in your case it was tar which executed without any issues means $? is 0..

You better check for directory inyour script with -d option of if..