How to lookup the meaning of exit codes for Linux command line utilities?

前端 未结 3 1407
北海茫月
北海茫月 2020-12-29 08:32

I have my prompt (bash) configured to print out the exit code from the last command, if it wasn\'t successful (aka not zero). Therefore I\'m seeing a lot of exit codes, even

相关标签:
3条回答
  • 2020-12-29 08:54

    There isn't a standardized meaning for the exit codes of programs beyond '0 is OK; anything else means something went wrong'. And strictly, that applies to C and C++ only - there, exit(0); or exit(EXIT_SUCCESS); both exit with success, but the value returned to the O/S might be different.

    There are exceptions to even the zero-on-success rule. Obviously, there are careless programs that don't return a defined exit status; such programs are best avoided. There are other not quite so careless programs that always return 0, even when something went wrong; they too are often best avoided.

    However, there are also programs that carefully encode quite a lot of information into the exit status, and simply getting a non-zero exit status does not mean such programs failed. Of course, the programs document the meanings of the exit statuses.

    POSIX is careful to document exit statuses for programs.

    The manual pages for a program should document the exit statuses. On Unix, if there is no such documentation, then assume zero-on-success and anything else is a failure.

    Note that if bash fails to execute a command, it will return stylized statuses:

    • 127 if the file does not exist or cannot be found
    • 126 if you do not have execute permission on the file

    Also, if the program dies because of a signal, bash lets you know by encoding the exit status as:

    • 128 + signal-number

    Hence SIGHUP yields 129, SIGILL yields 132, SIGTERM yields 143, etc. However, it is possible for a program to exit with any of those statuses and mean something different from what bash means. That said, it is a relatively unusual program that exits with any of those exit statuses, so you're usually safe.

    Note that different operating systems have different conventions: Unix provides an 8-bit status with zero for success; Windows provides a much larger range for the exit status values (16-bits or 32-bits); VMS used zero to mean failure, I believe.

    0 讨论(0)
  • 2020-12-29 08:56

    The man pages are the conventional place for this documentation. If you are on e.g. Debian, you would even be expected to file a bug report against utilities which have undocumented exit codes.

    0 讨论(0)
  • 2020-12-29 08:59

    The advanced Bash scripting guide also provides exit codes with special meanings.

    http://tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF

    0 讨论(0)
提交回复
热议问题