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.
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?
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.
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.