bash 如何查找 Linux 命令行实用程序的退出代码的含义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7294056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:42:45  来源:igfitidea点击:

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

bash

提问by chrm

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 when a program seems to encounter no problems. Is there a way to lookup the meaning of these exit codes?

我的提示 (bash) 被配置为打印出最后一个命令的退出代码,如果它不成功(也就是非零)。因此,我看到了很多退出代码,即使程序似乎没有遇到任何问题。有没有办法查找这些退出代码的含义?

I always try the man pages, info pages or the "--help" option, but to no avail.

我总是尝试使用手册页、信息页或“--help”选项,但无济于事。

To clarify, I'm asking about the utilities that come with Linux, like cd, ls, du, ...

为了澄清起见,我问的是 Linux 附带的实用程序,例如cd, ls, du, ...

回答by Jonathan Leffler

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.

程序的退出代码超过'0就可以了,没有一个标准化的含义;其他任何事情都意味着出了问题'。严格来说,这仅适用于 C 和 C++ - 在那里,exit(0);exit(EXIT_SUCCESS);两者都成功退出,但返回给 O/S 的值可能不同。

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.

即使是成功为零的规则也有例外。显然,有些粗心的程序不会返回定义的退出状态;最好避免此类程序。还有其他不太粗心的程序,即使出现问题,也总是返回 0;他们也往往最好避免。

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.

POSIX 小心地记录程序的退出状态。

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.

程序的手册页应该记录退出状态。在 Unix 上,如果没有这样的文档,那么假设成功为零,其他任何事情都是失败的。

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

请注意,如果bash无法执行命令,它将返回样式化状态:

  • 127 if the file does not exist or cannot be found
  • 126 if you do not have execute permission on the file
  • 127 如果文件不存在或找不到
  • 126 如果您没有对该文件的执行权限

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

此外,如果程序因信号而bash终止,请通过将退出状态编码为:

  • 128 + signal-number
  • 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 bashmeans. That said, it is a relatively unusual program that exits with any of those exit statuses, so you're usually safe.

因此,SIGHUP 产生 129,SIGILL 产生 132,SIGTERM 产生 143,等等。但是,程序有可能以任何这些状态退出并且意味着与 what mean 不同的bash含义。也就是说,这是一个相对不寻常的程序,它以任何这些退出状态退出,因此您通常是安全的。

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.

请注意,不同的操作系统有不同的约定:Unix 提供 8 位状态,成功为零;Windows 为退出状态值提供了更大的范围(16 位或 32 位);我相信,VMS 使用零表示失败。

回答by tripleee

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.

手册页是本文档的常规位置。如果您使用例如 Debian,您甚至需要针对具有未记录退出代码的实用程序提交错误报告。

回答by Ken

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

高级 Bash 脚本指南还提供了具有特殊含义的退出代码。

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

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