Re: Question with $? in bash, with chains of commands
> > smbtar -s $username -x $share -p $password -t - | gzip -1 > $filename.tar.gz
> > if [ $? -eq 0 ]; then
> > # rotate the backup;
> > fi;
> > # else go on to the next machine
> >
> > The problem is, $? reports the result of the last command, which is gzip,
> > which will ALWAYS report 0(well, unless the hd is full or the moon is
> > full), because smbtar spits out data to it. If I remove the gzip -1, it
> > works as expected.
The traditional method would be to use a flag file. Something like
this.
smbtar -s $username -x $share -p $password -t - \
|| touch /tmp/smbtar-failed.$$ | gzip -1 > $filename.tar.gz
if [ -f /tmp/smbtar-failed.$$ ]; then
: error
fi
rm -f /tmp/smbtar-failed.$$
You can see how that methodology works and I agree it is a little
clunky but it is functional and portable. If the first command failed
then you get a file. You made sure the file did not exist previously
so the presence thereof means you have trapped the error.
> man bash
>
> search for PIPESTATUS:
> PIPESTATUS
> An array variable (see Arrays below) containing a list of
> exit status values from the processes in the
> most-recently-executed foreground pipeline
> (which may contain only a single command).
Did not know about that extension. Pretty cool!
Bob
--
To UNSUBSCRIBE, email to debian-user-request@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Reply to: