Why do scripting languages use a reverse convention for indicating TRUE and FALSE?
In C, numerals have a boolean significance. Zero is FALSE as are its equivalents: the NULL pointer and the NULL character. Non-zero numbers are TRUE; in practice TRUE=1. This is clearly intuitive. Computers work in binary: 1 and 0, on and off, set and unset, pulse or no pulse. It seems logical that if there is no pulse when a pulse was expected, that signifies FALSE.
But when calling exit() in your program, you pass 0 for success and 1 for error or failure. That's because a script may need to analyse the behaviour of your program and all Unix scripting languages use that convention rather than the more intuitive one. I assume there must be some historical reason for that. Does anyone know how it originated? |
> But when calling exit() in your program, you pass 0 for success and 1 for error or failure.
Well, these aren't logical values, they are status-codes: 0 means success, other values are error codes. As many different errors may happen, it is quite plausible. (Edit: IBM /360 mainframe's programs often used these values: 0=OK, 4=Warning, 8=Error, 12+=FatalError) (I think it would be easier if program '/bin/true' would called 'success' and '/bin/false' would be 'fail'.) |
Quote:
Code:
void _exit(int status); So the error code is limited to the range 0(=successfull exit) thru 0377 octal (255 decimal), so is in fact an UNsigned byte. As the shell used does the "wait" call, it will get this 0 to 255 exit value from the application and that value is what the scripts are testing for. |
So "0" means "No errors". Yes, that would make sense. I think what confused me is that conditional structures in scripts (for example if...elif...else...fi in bash) treat the exit codes of programs in a boolean way and you can do boolean operations on them. They behave like TRUE and FALSE.
|
You can think of it as a boolean flag IS_ERROR
0 = FALSE | nonzero = TRUE It makes sense to have IS_ERROR rather than IS_SUCCESSFUL because you dont need to be specific about "there is no error". So 0 is the only FALSE but nonzero lets you be specific about things that can go into more detail (which nonzero) ... also C isnt different, so this isnt about scripting languages: https://en.wikipedia.org/wiki/Exit_status#C_language |
I suggest that one be extremely cautious assuming just values of 1 and 0. Some commands return other than just those two values. Yes a non-zero value means a problem, however it usually is beneficial to understand the difference between one error and another.
|
what everyone else says...
0 = nothing to report 1 = bust 2 = bust with more detail ... 404 = the page you are looking for does not exist and you can pass error codes Code:
#!/bin/bash |
It's because you are returning a useful value on error to the caller. All processes, not just shell scripts return a value, 0 means OK.
It's actually a combination of things, including if the process was killed etc, https://linux.die.net/man/2/waitpid https://en.wikipedia.org/wiki/Exit_status |
Quote:
Code:
perror("Open error"); (and, of course, these have as value 0 resp 1). Quote:
Code:
if <commandline> |
Quote:
|
Quote:
I remember reading in some programming guide or other that it's bad practice to have multiple error codes for different kinds of errors because nobody wants to have to look them up. The obvious exception is grep, where failure to find a string isn't really an error at all if the string isn't there. So grep has 0 for finding the string, 1 for not finding it and 2 for genuine errors such as bad syntax in search string. |
Quote:
So "if [ ..." can be written too as "test ...", which I often use as a one-line if: Code:
test -d $BASEDIR || mkdir $BASEDIR |
OK. "&&" and "||" are boolean operators, aren't they? So how does that work if the exit codes from test and rm are just integers?
|
read
https://mywiki.wooledge.org/BashGuid...ndConditionals but the quick answer is, you use case I wonder if I can find an example? here is one I made earlier https://www.linuxquestions.org/quest...1/#post6038551 |
Quote:
|
All times are GMT -5. The time now is 09:15 AM. |