|
| 1 | +#lang pollen |
| 2 | + |
| 3 | +◊page-init{} |
| 4 | +◊define-meta[page-title]{Exit Codes} |
| 5 | +◊define-meta[page-description]{Exit Codes With Special Meanings} |
| 6 | + |
| 7 | +◊definition-block[#:type "variables"]{ |
| 8 | +◊definition-entry[#:name "1"]{ |
| 9 | + |
| 10 | +Catchall for general errors |
| 11 | + |
| 12 | +Example: ◊code{let "var1 = 1/0"} |
| 13 | + |
| 14 | +Miscellaneous errors, such as "divide by zero" and other impermissible |
| 15 | +operations |
| 16 | + |
| 17 | +} |
| 18 | + |
| 19 | +◊definition-entry[#:name "2"]{ |
| 20 | +Misuse of shell builtins (according to Bash documentation) |
| 21 | + |
| 22 | +Example: ◊code{empty_function() ◊escaped{◊"{"}◊escaped{◊"}"}} |
| 23 | +
|
| 24 | +Missing keyword or command, or permission problem (and diff return |
| 25 | +code on a failed binary file comparison). |
| 26 | +
|
| 27 | +} |
| 28 | +
|
| 29 | +◊definition-entry[#:name "126"]{ |
| 30 | +
|
| 31 | +Command invoked cannot execute |
| 32 | +
|
| 33 | +Example: ◊code{/dev/null} |
| 34 | +
|
| 35 | +Permission problem or command is not an executable |
| 36 | +
|
| 37 | +} |
| 38 | +
|
| 39 | +◊definition-entry[#:name "127"]{ |
| 40 | +
|
| 41 | +"command not found" |
| 42 | +
|
| 43 | +Possible problem with ◊code{$PATH} or a typo |
| 44 | +
|
| 45 | +} |
| 46 | +
|
| 47 | +◊definition-entry[#:name "128"]{ |
| 48 | +
|
| 49 | +Invalid argument to exit |
| 50 | +
|
| 51 | +Example: ◊code{exit 3.14159} |
| 52 | +
|
| 53 | +◊command{exit} takes only integer args in the range 0 - 255 (see first |
| 54 | +footnote) |
| 55 | +
|
| 56 | +} |
| 57 | +
|
| 58 | +◊definition-entry[#:name "128+n"]{ |
| 59 | +
|
| 60 | +Fatal error signal "n" |
| 61 | +
|
| 62 | +Example: ◊code{kill -9 $PPID} |
| 63 | +
|
| 64 | +◊code{$?} returns 137 (128 + 9) |
| 65 | +
|
| 66 | +} |
| 67 | +
|
| 68 | +◊definition-entry[#:name "130"]{ |
| 69 | +
|
| 70 | +Script terminated by Control-C |
| 71 | +
|
| 72 | +Example: ◊kbd{Ctl-C} |
| 73 | +
|
| 74 | +Control-C is fatal error signal 2, (130 = 128 + 2, see above) |
| 75 | +
|
| 76 | +} |
| 77 | +
|
| 78 | +◊definition-entry[#:name "255*"]{ |
| 79 | +
|
| 80 | +Exit status out of range |
| 81 | +
|
| 82 | +Example: ◊code{exit -1} |
| 83 | +
|
| 84 | +◊code{exit} takes only integer args in the range 0 - 255 |
| 85 | +
|
| 86 | +} |
| 87 | +
|
| 88 | +} |
| 89 | +
|
| 90 | +According to the above table, exit codes 1 - 2, 126 - 165, and 255 |
| 91 | +have special meanings, and should therefore be avoided for |
| 92 | +user-specified exit parameters. Ending a script with exit 127 would |
| 93 | +certainly cause confusion when troubleshooting (is the error code a |
| 94 | +"command not found" or a user-defined one?). However, many scripts use |
| 95 | +an exit 1 as a general bailout-upon-error. Since exit code 1 signifies |
| 96 | +so many possible errors, it is not particularly useful in |
| 97 | +debugging. ◊footnote{Out of range exit values can result in unexpected |
| 98 | +exit codes. An exit value greater than 255 returns an exit code modulo |
| 99 | +256. For example, ◊code{exit 3809} gives an exit code of 225 (3809 % 256 = |
| 100 | +225).} |
| 101 | +
|
| 102 | +There has been an attempt to systematize exit status numbers (see |
| 103 | +◊fname{/usr/include/sysexits.h}), but this is intended for C and C++ |
| 104 | +programmers. A similar standard for scripting might be |
| 105 | +appropriate. The author of this document proposes restricting |
| 106 | +user-defined exit codes to the range 64 - 113 (in addition to 0, for |
| 107 | +success), to conform with the C/C++ standard. This would allot 50 |
| 108 | +valid codes, and make troubleshooting scripts more |
| 109 | +straightforward. All user-defined exit codes in the accompanying |
| 110 | +examples to this document conform to this standard, except where |
| 111 | +overriding circumstances exist, as in TODO Example 9-2. ◊footnote{An |
| 112 | +update of ◊fname{/usr/include/sysexits.h} allocates previously unused |
| 113 | +exit codes from 64 - 78. It may be anticipated that the range of |
| 114 | +unallotted exit codes will be further restricted in the future. The |
| 115 | +author of this document will not do fixups on the scripting examples |
| 116 | +to conform to the changing standard. This should not cause any |
| 117 | +problems, since there is no overlap or conflict in usage of exit codes |
| 118 | +between compiled C/C++ binaries and shell scripts.} |
| 119 | +
|
0 commit comments