3 ms·
Is 2 used consistently by any programs in this manner? If I was to advise someone I'd tell o interpret and write programs using the following rule of thumb and
by Normal_gaussian 5y ago
Is 2 used consistently by any programs in this manner?
If I was to advise someone I'd tell o interpret and write programs using the following rule of thumb and not to worry too much:
0 = success / true
1 = failure (read stderr) / false
>1 = failure (read stderr & docs)
- ttyprintk 5y agoBash uses 1 and 2 this way.
- spicybright 5y agoThis is the first I'm hearing of using code 2 myself. Been using bash casually for years now.
- epage 5y agoThe main arg parser for Rust (clap) was looking at using sysexits for usage but then got feedback from burntsushi (of ripgrep fame) for how rarely it is used and instead encouraged 2. In looking at it, it seems like 2 is the most consistent code and for clap v3 will be using it. - 2 is "misuse of shell built-ins" for bash [1] - Python's argparse uses 2 [2] I feel like we looked at more but can't remember which all they were [0] https://github.com/clap-rs/clap/pull/1637#issuecomment-581032396 https://github.com/clap-rs/clap/pull/1637#issuecomment-58103... [1] https://tldp.org/LDP/abs/html/exitcodes.html https://tldp.org/LDP/abs/html/exitcodes.html [2] https://github.com/python/cpython/blob/main/Lib/argparse.py#L2580 https://github.com/python/cpython/blob/main/Lib/argparse.py#...
- jitl 5y agoMany programming languages will exit 1 for unexpected exceptions at runtime (or programmer error). Eg, this Ruby program will exit 1: $ ruby -e 'raise StandardError.new "oops"' Traceback (most recent call last): -e:1:in `<main>': oops (StandardError) $ echo $? 1 And so will this one: $ ruby -e 'invalid method call' Traceback (most recent call last): -e:1:in `<main>': undefined local variable or method `call' for main:Object (NameError) Did you mean? caller So, it's nice to distinguish between a possibly unexpected exceptional exit (1), and an "error case detected by the programmer", which is what I use exit (2) for.
- umvi 5y agoI do something very similar, though I read I should avoid codes 1 and 2 so my error codes start at 3: https://github.com/RPGillespie6/fastcov/blob/master/fastcov.py#L47 https://github.com/RPGillespie6/fastcov/blob/master/fastcov.... That way automated CI pipelines (or whatever) can switch on the return code if needed rather than try to parse stderr
- LukeShu 5y agoThe Linux Standard Base (LSB) specification specifies this meaning of 2 for init scripts, and systemd interprets daemon exit codes in accordance with LSB. From there, this meaning of 2 has spread to other types of programs and is quite common, but still far from ubiquitous. https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LS...