目录
当前位置: 首页 > 文档资料 > Shell 中文文档 >

Appendix D. Exit Codes With Special Meanings

优质
小牛编辑
124浏览
2023-12-01

Table D-1. "Reserved"Exit Codes

Exit Code Number Meaning Example Comments
1 Catchall for general errors let "var1 = 1/0" Miscellaneous errors, such as "divide byzero"
2 Misuse of shell builtins (according to Bash documentation)   Seldom seen, usually defaults to exit code

1126Command invoked cannot executePermission problem or command is not an executable127"command not found"Possible problem with $PATHor a typo128Invalid argument to exitexit 3.14159exittakes only integer args in therange0 - 255(seefootnote)128+nFatal error signal "n"kill -9$PPIDof script$?returns

137(128 + 9)130Script terminated by Control-CControl-C is fatal error signal

2, (130 = 128 + 2, see above)255*Exit status out of rangeexit-1exittakes only integer args in therange0 - 255

According to the above table, exit codes1 - 2,126 - 165, and 255

[1]

have special meanings, and should therefore be avoided foruser-specified exit parameters. Ending a script with exit127would certainly cause confusion when troubleshooting(is the error code a "command not found"or auser-defined one?). However, many scripts use an exit1as a general bailout upon error. Since exit code

1signifies so many possible errors,this probably would not be helpful in debugging.

There has been an attempt to systematize exit status numbers(see /usr/include/sysexits.h),but this is intended for C and C++ programmers. A similarstandard for scripting might be appropriate. The author ofthis document proposes restricting user-defined exit codes tothe range64 - 113(in addition to

0, for success), to conform withthe C/C++ standard. This would allot 50 valid codes, and maketroubleshooting scripts more straightforward.

All user-defined exit codes in the accompanying examplesto this document now conform to this standard, exceptwhere overriding circumstances exist, as in Example 9-2.

Issuing a $?fromthe command line after a shell script exits gives resultsconsistent with the table above only from the Bash orshprompt. Running the C-shell ortcshmay give different values in somecases.

Notes

[1]

Out of range exit values can result inunexpected exit codes. An exit value greaterthan255returns an exitcode modulo

256. For example, exit3809gives an exit code of225(3809 % 256 = 225).