位置: 编程技术 - 正文

exit(-1)或者return(-1)shell得到的退出码为什么是255(exit(1)和return的区别)

编辑:rootadmin

推荐整理分享exit(-1)或者return(-1)shell得到的退出码为什么是255(exit(1)和return的区别),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:exit(0)与return0,exit(0)和return 0,exit(1)和return的区别,exit(0) return 0,exit _exit,exit(0)与return0,exit0和return0,exit(0)与return0,内容如对您有帮助,希望把文章链接给更多的朋友!

下面我写了一个hello world程序,一起看看吧:

编译执行:gcc main.c && ./a.out

现在我们看看在当前shell中返回上一个执行过程的返回值是多少,是 “-1” 吗?

inuyasha@inuyasha-Aspire-:~/桌面$ gcc main.c && ./a.out

hello world!

inuyasha@inuyasha-Aspire-:~/桌面$ echo $&#;

啊,结果为什么 ""呢?调用一个程序, 程序退出-1, 得到结果不是“-1”吗?

exit(-1)或者return(-1)shell得到的退出码为什么是255(exit(1)和return的区别)

以下引用自: 是因为exit或者main函数中的return, 只能使用0~之间的值. -1 的unsigned值就是.

那么复杂点的说呢&#;

我们知道, 在Shell中, 运行一个命令, 一个程序, 都是fork一个子进程(然后exec)来执行的, 而这个程序的退出码, 被Shell(父进程), 通过wait来收集而后报告给我们的.

pid_twait(int *statloc);

而对于wait来说, 历史上原因, 他将通过statloc返回一个bit的interge(现在也有用位表示的, 但是会兼容已有的设计). 这bits的interge中, 高8位就是程序退出的值(exit, 或者return), 而低八位表示导致这个程序退出的信号(其中一位表示是否有Core文件产生), 如果程序是正常退出, 那么低八位为0[1].

所以, 如果我们返回-1, 并且因为我们是正常退出, 所以Shell通过wait收集到的子进程退出状态是:

而高八位作为unsigned, 就是了.

另外, 补充一下, 在Linux的内建Shell命令中, 很多都会遵守一个退出状态码的约定, 具体的值对应的意思[2]:

Exit Code Number Meaning Example Comments 1 Catchall for general errors let “var1 = 1/0″ Miscellaneous errors, such as ”divide by zero” and other impermissible operations 2 Misuse of shell builtins (according to Bash documentation) empty_function() {} Seldom seen, usually defaults to exit code 1 Command invoked cannot execute Permission problem or command is not an executable “command not found” illegal_command Possible problem with $PATH or a typo Invalid argument to exit exit 3. exit takes only integer args in the range 0 ? (see first footnote) +n Fatal error signal ”n” kill -9 $PPID of script $&#; returns ( + 9) Script terminated by Control-C Control-C is fatal error signal 2, ( = + 2, see above) * Exit status out of range exit -1 exit takes only integer args in the range 0 ?

Linux rpm 命令参数使用详解 rpm执行安装包二进制包(Binary)以及源代码包(Source)两种。二进制包可以直接安装在计算机中,而源代码包将会由RPM自动编译、安装。源代码包经常

linux shell 字符串操作(长度,查找,替换)详解 在做shell批处理程序时候,经常会涉及到字符串相关操作。有很多命令语句,如:awk,sed都可以做字符串各种操作。其实shell内置一系列操作符号,可以达

linux whatis与whatis database 使用及查询方法(man使用实例) 通过man查找帮助过程:[chengmo@centos5~]$man-h...f:sameaswhatis(1)...#觉得比较奇怪,whatis是什么呢?[chengmo@centos5~]$manwhatis#查询得到是:#whatis-searchthewhatisdatabasefo

本文链接地址:https://www.jiuchutong.com/biancheng/371368.html 转载请保留说明!

上一篇:iredmail下安装脚本分析(一)---get_all.sh 文件所在目录为PKGS(iredmail是免费的吗)

下一篇:Linux rpm 命令参数使用详解(linux rpm -i)

免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

鄂ICP备2023003026号

网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

友情链接: 武汉网站建设 电脑维修 湖南楚通运网络