Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 列出所有系统退出状态代码及说明_Linux_Bash_Shell_Unix_Exit - Fatal编程技术网

Linux 列出所有系统退出状态代码及说明

Linux 列出所有系统退出状态代码及说明,linux,bash,shell,unix,exit,Linux,Bash,Shell,Unix,Exit,我从shell中知道$?保存最后执行的程序退出状态 例如,当我运行下面的命令时,我看到了不同情况下的不同状态 test$ hello -bash: hello: command not found test$ echo $? 127 test$ expr 1 / 0 expr: division by zero test$ echo $? 2 我想知道在系统或互联网上是否有任何常见的退出状态列表,我可以从中获得所有的退出状态及其描述。我找到了一个列表,但缺少一些代码,例如状态代码127您显示的

我从shell中知道
$?
保存最后执行的程序
退出状态

例如,当我运行下面的命令时,我看到了不同情况下的不同状态

test$ hello
-bash: hello: command not found
test$ echo $?
127
test$ expr 1 / 0
expr: division by zero
test$ echo $?
2

我想知道在系统或互联网上是否有任何常见的退出状态列表,我可以从中获得所有的退出状态及其描述。我找到了一个列表,但缺少一些代码,例如
状态代码127

您显示的列表实际上是最接近“标准化”的列表,但坦率地说,它看起来比实际更合法。据我所知,几乎没有人关注这些家伙,但每个人都会说出自己的退出状态:

执行test1.sh

#!/bin/bash
a=10 ; [ "$a" -eq 9 ] && echo "Cool!" || exit 200 
输出:

:~$ test1.sh
:~$ echo $?
200

退出状态是程序返回给调用程序或shell的数值。在C程序中,这由
main()
函数的返回值或您给
exit(3)
的值表示。数字中唯一重要的部分是最低有效8位,这意味着只有
0
255
之间的值

Code    Description
0       success
1-255   failure (in general)
126     the requested command (file) can't be executed (but was found)
127     command (file) not found
128     according to ABS it's used to report an invalid argument to the exit 
        builtin, but I wasn't able to verify that in the source code of Bash 
        (see code 255)
128 + N the shell was terminated by the signal N (also used like this by 
        various other programs)
255     wrong argument to the exit builtin (see code 128)
较低的代码
0
125
不保留,可用于程序想要报告的任何内容。值0表示成功终止,值非0表示不成功终止。这种行为
(==0,!=0)
也是Bash在一些代码流控制语句(如
if
while
)中的反应


上述摘录摘自。

的章节,不可能有全面的列表,因为命令退出状态的含义本质上是特定于命令的。对于给定的命令,您通常可以在相应命令的手册页和信息文档中获取有关信息

test$ hello
-bash: hello: command not found
test$ echo $?
127
test$ expr 1 / 0
expr: division by zero
test$ echo $?
2
退出代码
127
来自
bash
,因为无法找到请求的命令本身

test$ hello
-bash: hello: command not found
test$ echo $?
127
test$ expr 1 / 0
expr: division by zero
test$ echo $?
2
退出代码
2
来自
expr

这些命令中的一些可能是标准化的,或者至少是针对多个命令或一组命令进行协调的(例如,我可以想象“
sh
-兼容shell”),但除非某个命令想要符合其中一个约定(并且可能存在多个冲突约定),命令的作者完全可以自由决定他们希望退出状态代码的含义

有一个重要的例外:所有UNIX命令都应该遵守这一松散的规则才能成为好公民,并在命令行上提供有意义的可组合性(例如,使用管道):

  • 0
    表示“成功”或“真实”/“真实”
  • 0
    表示(在非常广泛的意义上)“失败”或“不成功”或“错误”/“错误”
正如您所看到的,这仍然为解释留下了很大的空间,这是完全有意的,因为这些含义必须特定于各个命令的上下文。(例如,考虑
false
命令,其目的就是“失败”,因此总是返回非
0
退出代码。)


您找到的描述了系统调用的返回代码。系统调用是当程序向内核发出请求(in)时进行的,与命令调用不同,因此这些返回代码(不一定)与命令退出代码相同。

值得注意的是,退出状态1几乎总是表示“一般错误”,而不是指出任何特定类型的错误。人们非常关注该列表;它不是进程退出状态的列表,而是系统调用返回值的列表。