Linux如何通过错误号获取错误描述 [英] Linux How to get error description by error number
问题描述
在linux系统中(特别是我有Ubuntu 14),如果某些程序因错误而终止,我可以通过$?
变量获取数字错误代码
In linux (specifically I have Ubuntu 14) if some program terminates with an error, I can get numerical error code via $?
variable
$ ./failing_app
$ echo $?
但是数字本身并不能告诉我太多,如何获得错误名称和描述?
However the number itself does not tell me much, how do I get error name and description?
$ man errno
中有一个错误列表,但仅提供名称,而不提供数值.
There is a list of errors in $ man errno
but it gives only names, not numerical values.
我已经搜索过google,结果非常奇怪:例如我有一个玩具Rust程序,在其中我试图越界访问数组元素.程序出现紧急情况,$?
是101
,但是据说它与Network unreachable
错误相对应,这没有任何意义.
I've search the google and results are quite strange: e.g. I have a toy Rust program in which I'm trying to access array element out of bounds. The program panics and $?
is 101
, however it is said to correspond to Network unreachable
error, which doesn't make any sense.
推荐答案
程序的退出状态(shell中的$?
)与C errno
无关.
The exit status of a program ($?
in the shell) is unrelated to the C errno
.
在C程序中,退出状态通常来自exit
的自变量或main
的返回值.约定是0表示成功退出(对于shell来说为true),而其他值则为失败(ir为false).
In a C program, normally the exit status comes from the argument to exit
or the return value of main
. The convention is that 0 means a successful exit (or true for the shell) and other values are a failure (ir false).
但是,如果程序因接收到信号而死亡,则外壳程序会将$?
设置为128加信号号.例如,在出现分段错误(SIGSEGV
为11)时,$?
将为139.
However if a program died from receiving a signal, the shell sets $?
to 128 plus the signal number. For example, on a segmentation fault (SIGSEGV
, which is 11), $?
would be 139.
要列出信号编号,请运行kill -l
.
To list signal numbers, I run kill -l
.
这篇关于Linux如何通过错误号获取错误描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!