MLUG: Re: [MLUG] basic Unix pipe/exit code problem
Re: [MLUG] basic Unix pipe/exit code problem
Email address obfuscation in effect -- please click here to turn it off.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
On Thu, 3 Mar 2005 12:54:35 -0600 (CST), Mike Miller
<EMAIL:PROTECTED> wrote:
> On Thu, 3 Mar 2005, Mark Rages wrote:
> 
> > I have reduced my problem to this:
> >
> > ( /bin/false | gzip > /dev/null ) || echo fail
> >
> > I want it to echo "fail" because /bin/false gives a nonzero exit code.
> > But it doesn't.  What am I missing?
> 
> Maybe I'm not understanding this.  If you are gzipping from stdin, don't
> you need to do it this way:
> 
>      | gzip -c - >
> 
> Maybe that isn't your intention.  I'm not sure how gzip is used in a path
> when it isn't reading stdin and writing to stdout.
> 
> I don't quite understand 'false'.  Where is the exit code detected?  It
> doesn't go to stderr or to stdout.

A little more explanation, then.

Unix programs return a numeric code to the shell when they finish. 
This code is 0 when the program finishes successfully, and an error
code when the program fails.

/bin/false exits immediately with a nonzero exit code.  ( /bin/true
exits with 0 )

I was using /bin/false only as an example of a failing command.  I was
trying to explain my situation where I'm executing a string of
commands piped together, and I want to know if one of the commands
failed or not.  (this is a script for making backups, so I can't just
let it silently fail!)

What I thought (appears to be wrong) is that if a command at the
beginning of a pipe fails, it will send SIGPIPE to the next command,
which will kill it with an error, and so on, until the shell will
receive an error code.  This is obviously incorrect.  Can some Unix
guru explain this to me?

Regards,
Mark
EMAIL:PROTECTED
-- 
You think that it is a secret, but it never has been one.
  - fortune cookie
_______________________________________________
members mailing list
EMAIL:PROTECTED
http://mlug.missouri.edu/mailman/listinfo/members