Linux Shell ERROR/RETURN code

  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
  126         Command invoked cannot execute    Permission problem or command is not an executable
  127        “command not found” illegal_command Possible problem with $PATH or a typo
  128         Invalid argument to exit  exit 3.14159  exit takes only integer args in the range 0 – 255 (see first footnote)
  128+n    Fatal error signal ”n”  kill -9 $PPID of script $? returns 137 (128 + 9)
  130        Script terminated by Control-C    Control-C is fatal error signal 2, (130 = 128 + 2, see above)

  255*      Exit status out of range  exit -1 exit takes only integer args in the range 0 – 255


退出码的值 
1 通用错误  let "var1 = 1/0"  各种各样的错误都可能使用这个退出码, 比如"除0错误"
2 shell内建命令使用错误(Bash文档上有说明)   很少看到, 通常情况下退出码都为1
126 命令调用不能执行    程序或命令的权限是不可执行的
127 "command not found"   估计是$PATH不对, 或者是拼写错误
128 exit的参数错误  exit 3.14159  exit只能以整数作为参数, 范围是0 - 255(见脚注)
128+n 信号"n"的致命错误 kill -9 脚本的$PPID $? 返回137(128 + 9)
130 用Control-C来结束脚本   Control-C是信号2的致命错误, (130 = 128 + 2, 见上边)
255*  超出范围的退出状态  exit -1 exit命令只能够接受范围是0 - 255的整数作为参数
通过上面的表, 我们了解到, 退出码1 - 2, 126 - 165, 和255 [1] 都具有特殊的含义, 因此应该避免使用用户指定的退出参数. 如果脚本使用exit 127作为退出语句, 那么可能就会在故障诊断的时候产生混淆(如何判断这是由"command not found"引起的, 还是由用户定义引起的?). 然而, 许多脚本使用exit 1作为通用的返回错误值. 因为退出码1能够表示的错误太多了, 不过这么做, 对于调试来说, 也起不到任何帮助的作用.
其实早就有人对退出状态值进行了系统的分类(请参考/usr/include/sysexits.h), 不过这个文件是为C/C++程序员准备的. 其实shell脚本也需要这样一个类似的标准. 所以本文作者呼吁限制使用用户定义的退出码, 尤其是范围64 - 113(还有
0, 表示成功), 这么做, 就可以和C/C++标准保持一致. 这样我们就有了50个可用的退出码, 而且非常便于故障诊断.
本书中所有例子中的用户定义退出码都符合这个标准, 除了那些超出标准范围的例子, 比如例子 9-2.


  只有在Bash或sh提示符下, 当shell脚本退出后, 在命令行上使用$?才会得到与上表相一致的结果. 在某些情况下, 运行C-shell或者tcsh可能会给出不同的值.
  注意事项
  [1]
  超出范围的退出值可能会产生意想不到的退出码. 如果退出值比255大, 那么退出码将会取256的模. 举个例子, exit 3809的退出码将是225(3809 % 256 = 225).


退出码的值

 1    通用错误 let "var1 = 1/0" 各种各样的错误都可能使用这个退出码, 比如"除0错误"

 2    shell内建命令使用错误(Bash文档上有说明) 很少看到, 通常情况下退出码都为1。所有内建命令返回 2 来指示不正确的用法。

 126   命令调用不能执行 程序或命令的权限是不可执行的

 127   "command not found" 估计是$PATH不对, 或者是拼写错误

 128   exit的参数错误 exit 3.14159 exit只能以整数作为参数, 范围是0 - 255

 128+n 信号"n"的致命错误 kill -9 脚本的$PPID $? 返回137(128 + 9)

 130   用Control-C来结束脚本 Control-C是信号2的致命错误, (130 = 128 + 2, 见上边)

 255+  超出范围的退出状态 exit -1 exit命令只能够接受范围是0 - 255的整数作为参数。如果退出值比255大, 那么退出码将会取256的模. 举个例子, exit 3809的退出码将是225(3809 % 256 = 225).

返回值分析

   1:程序中,用 exit 来设置进程的退出值时,虽然该参数的类型为 int,但在父进程中,只能取得其值的低 8 位,所以用 exit 返回时,高于 255 是没有意义的。
   2:对应 system 返回值,其值由两部分组成:低 8 位表示所执行的脚步在执行过程中所接收到的信号值,其余的位表示 exit 的返回值。
   3:由 2 可知,对于 system 返回值(ret)来讲,ret>>8 即为 exit 返回值,也对应为获取的 shell 命令的执行状态($?)。
   4:return : Perl 与 Shell(Bash)是不同的

      Bash只能返回0-255的值,但可以通过echo返回字符串和整数

      Perl 可以通过return返回字符串,数字(整数和小数)


system, 反引号,qx

1: 反勾号返回的是命令行返回的print的数值和出错代码,标准输出不会输出到标准输出。

2: system这个函数为你执行任何系统里的程序并返回该程序的退出状态——而不是它的输出。要捕获命令行上的输出,你应该用反勾号或者 qx//。system的输出直接输出到标准输出上。

3: qx// 返回的是命令行返回的标准输出,不含标准错误, 退出代码在 $? 中,但和 终端 中的退出代码不同。

4: system, qx//, 反引号 的错误都会输出到标准错误上,不管是不是在管道中。

带有管道的命令组合

1: 命令从左到右依次执行,直到所有命令执行完毕或遇到致命错误。

2: 左边的命令对标准输出上的输出是它所链接的命令的输入。如果没有重定向,标准错误直接输出到标准错误输出上

3: return value : 最后一个命令的返回值

4: ssh: 在没有重定向时,标准输出和标准错误输出分别输出到标准输出和标准错误输出

AND and OR

cmd1 && cmd2: 只有当cmd1的退出码是 0 时,cmd2才会执行

cmd1 || cmd2  : 只有当cmd1的退出码是 非零 时,cmd2才会执行

AND and OR exit status: 最后一个命令的退出码。

至于Linux的退出码为什么限定在0-255,现在也没找到说明文档。man bash 只有规定而没有说为什么这么规定.如有知道的,谢谢告知


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
################################################################################ # # # Super Ftp Server and Client for Wall-e # # # # SatMay30 2009 # # # ################################################################################ ////////\\\\\\\ ||Introduct || \\\\\\\\/////// Wall-e is a team for a project about two-demision code , for details at this:http://hi.baidu.com/hzau_wall_e this server and client programs is created for the s3c2410 platform board to conveniencely control it and fulfil files transferring at the same time , that is a simple combination for telnet and ftp server. Of course there are many fails and bugs in it , I'll appreciate the correction and criticism! ////////\\\\\\\ ||Functions || \\\\\\\\/////// 1 no count and no authentication , the shell running level result in who run the server in server side 2 basic ftp transferring functions as get files (g files ) and send files(s fname). 3 running common shells in client side(!cmd) and server side(cmd) ////////\\\\\\\ || Usage || \\\\\\\\/////// 1 run the server and client server: ./server_x # x is for your platform h for host pc, a for arm, as details to see Makefile client: ./client_x xx.xx.xx.xx then you can operate in the client side. the prompt of client side will be: Walle> 2 run server side shells [usage] : cmd [return]: return what the shell will return to stdout and stderr . [eg]: Walle>ls -l 总计 152 -rwxr-xr-x 1 root root 17382 05-30 22:00 client_a -rwxr-xr-x 1 root root 13060 05-30 22:00 client_h -rw-r--r-- 1 root root 4485 05-30 16:01 common.h -rwxr-xr-x 1 root root 462 05-30 21:43 Makefile -rw-r--r-- 1 root root 579 05-30 22:00 README -rw-r--r-- 1 root root 577 05-30 22:00 README~ -rwxr-xr-x 1 root root 20835 05-30 22:00 server_a -rwxr-xr-x 1 root root 16176 05-30 22:00 server_h -rwxr-xr-x 1 root root 5225 05-30 21:47 sock_client.c -rwxr-xr-x 1 root root 4362 05-30 15:09 sock_server.c Walle>cd /root Walle>pwd /root Walle> Walle>get aaa sh: get: command not found Walle> 3 run client side local shells [usage] :! cmd [return]: return what the shell will return to stdout and stderr . [eg] : Walle>!ls /root a es OperaDownloads aa f Pictures anaconda-ks.cfg fcitx Public automake_cn.htm g save_usb b install server_a client_h install.log server_h common.c install.log.syslog src common.h~ ked tcpd-0.2.0.tar.gz dairy Music Templates Desktop my_fs tmp Documents myos Unsaved Document 1 4 send files [usage] :s localfilename [return]: return successfully message,if success, or print error messages error message will be introduced below [eg] : Walle>! cd /usr/src/ Walle>s qt-embedded-linux-opensource-src-4.4.3.tar.gz send files successfully! Walle>ls -l ...... 1 root root 124238957 05-30 22:09 qt-embedded-linux-opensource-src-4.4.3.tar.gz Walle> tar xzvf qt-embedded-linux-opensource-src-4.4.3.tar.gz ...... 5 get files [usage] : g remotefilename [return]: return successfully message,if success, or print error messages error message will be introduced below [eg] : Walle> ls .... Walle>g tcpd-0.2.0.tar.gz get remote files successfully! Walle> 5 error return xsh:cmd: xxx xxx : error return from shells F_N_EST : file does not exists! SOCK_SEND_ERR : send socket data err! case S_RPC_TIMEOUT : socket rpc timeout!(wait for 5 secs no sequential data arrives from server) case F_C_ERR : file create error! default : unknown error! 6 bugs both side couldn't send control signal to each other, that is you can only send message that after a line break. so there are many shells programs couldn't be runned by client side: vi, ex,ed ,top ... , I'm really sorry for this. 7 contact if you've any questions please send Email to me at [email protected] or chat me by QQ : 547268476, I'll appreciate it !

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值