Capture error before pipe

Hi,

I have a script that runs a tar command to standard out then pipes to a gzip:

tar cfE - * | gzip -c > OUT.gz

At the moment, even if the tar fails (e.g. because of lack of disk space), the gzip still runs successfully.

Is there a way to make the whole line exit with a non-zero error status if the tar fails? I have to run it on a single line, because it is tar'ing a large directory.

Thanks

In this case, are you looking for errors such as tar being unable to read files etc.?

You might get away with something like this:-

tar cfE - * 2>/tmp/tar.log | gzip -c > OUT.gz
gzip_RC=$?

tar_RC=$(wc -l < /tmp/tar.log)


if [ $gzip_RC -ne 0 ]
then
   echo "The gzip failed RC=$gzip_RC"
fi


if [ $tar_RC -ne 0 ]
then
   echo "The tar failed RC=$tar_RC"
fi

You don't tell us your OS, so what does your manual page say the E flag is for?

Kind regards,
Robin

Me too, I'd like to know more about the E option . . .
If your shell (which you fail to mention, btw) were bash , did you consider the

echo ${PIPESTATUS[@]} 
2 0

shell variable array?

1 Like

Sorry - I'm using Solaris 10 and 11. The man page explains it better than I can:

I use it because there is a limit on the number of files that tar cf accepts, that the E flag extends.

It hadn't occurred to me to send standard error to another file; that sounds like the best solution for my case, because I am somewhat limited in what I can run. What I am actually doing here is running an Ingres database backup and changing the backup template file.

Thanks for your help - I'll give it a go.

Jackie

With bash3 or up, zsh and a recent ksh93, you can also use pipefail:

$ set -o pipefail; false | true ; echo "$?"
1

On Solaris 10, what shells/versions do you have available?

1 Like

In that case, why not just tar -cf - . ? Captures the entire current directory without having to pass in a literal 100,000 files.

Or perhaps even tar -zcf out.gz .

You did not tell us which shell you are using to execute the pipe. At least with bash and Zsh, you have access to the exit status of each process in the pipe. For bash, search for PIPESTATUS in the man-page. For Zsh, it is pipestatus.