Am Freitag, 25. Oktober 2002 04:29 schrieb Bill Shirley:
> Thanks for your reply.  It really clears things up.
>
> > In shell scripts, 0 is true, 1 (or any other non-zero) is false,
> > allowing for error codes.
>
> How counter intuitive!!  I have had beginning C programming, (so
> I'm an expert now :-) and false is zero and true is anything not
> false. It's a crazy world we live in.
The key point is the "allowing for error code" part. And actually many C 
functions don't use 0 => false, 1 => true either. Three basic types of 
returning results from functions come to mind:
1) int foo( void ); returns non-zero on success, zero on failure. This sticks 
to the natural "false is 0, true is != 0" idiom and is mostly used for 
functions that return an exact error code somewhere else and some useful 
value on success.
2) int foo( void ); returns > 0 on success, < 0 (often -1) on failure. These 
are used just like 1) when 0 is in the set of meaningful successful return 
values.
3) int foo( void ); returns 0 on success and != 0 (often an error code) on 
failure. This is often used to commucate an error code in the return value 
and on success, no value is returned anyway - or some of the arguments are 
output parameters.

Only 1) implements 0 == false, 1 == true (or rather 0 == false, 0 != true). So 
why should the shell stick to 1) and not 3) ? It's pretty unusual that a 
program wants to returns a meaningful value in its exit status *on success*. 
That's what stdout is for.

Just my 0.02€,
-Malte


Reply via email to