设为首页 收藏本站
查看: 794|回复: 0

shell 程序 返回码 退出码

[复制链接]

尚未签到

发表于 2018-8-19 13:10:21 | 显示全部楼层 |阅读模式
  用途说明
  exit命令用于退出当前shell,在shell脚本中可以终止当前脚本执行。
  常用参数
  格式:exit n
  退出。设置退出码为n。(Cause the shell to exit with a status of n.)
  格式:exit
  退出。退出码不变,即为最后一个命令的退出码。(If n is omitted, the exit status is that of the  last  command executed. )
  格式:$?
  上一个命令的退出码。
  格式:trap "commands" EXIT
  退出时执行commands指定的命令。( A trap on EXIT is executed before the shell terminates.)
  退出码(exit status,或exit code)的约定:
  0表示成功(Zero - Success)
  非0表示失败(Non-Zero  - Failure)
  2表示用法不当(Incorrect Usage)
  127表示命令没有找到(Command Not Found)
  126表示不是可执行的(Not an executable)
  >=128 信号产生
  man 3 exit 写道
  The C standard specifies two constants, EXIT_SUCCESS and EXIT_FAILURE, that may be passed to exit() to indicate
  successful or unsuccessful termination, respectively.
  以下摘自/usr/include/stdlib.h
  C代码
  1. #define EXIT_FAILURE    1       /* Failing exit status.  */
  2. #define EXIT_SUCCESS    0       /* Successful exit status.  */
  BSD试图对退出码标准化。
  man 3 exit 写道
  BSD has attempted to standardize exit codes; see the file .
  以下摘自/usr/include/sysexits.h
  C代码
  1. #define EX_OK           0       /* successful termination */
  2.
  3. #define EX__BASE        64      /* base value for error messages */
  4.
  5. #define EX_USAGE        64      /* command line usage error */
  6. #define EX_DATAERR      65      /* data format error */
  7. #define EX_NOINPUT      66      /* cannot open input */
  8. #define EX_NOUSER       67      /* addressee unknown */
  9. #define EX_NOHOST       68      /* host name unknown */
  10. #define EX_UNAVAILABLE  69      /* service unavailable */
  11. #define EX_SOFTWARE     70      /* internal software error */
  12. #define EX_OSERR        71      /* system error (e.g., can't fork) */
  13. #define EX_OSFILE       72      /* critical OS file missing */
  14. #define EX_CANTCREAT    73      /* can't create (user) output file */
  15. #define EX_IOERR        74      /* input/output error */
  16. #define EX_TEMPFAIL     75      /* temp failure; user is invited to retry */
  17. #define EX_PROTOCOL     76      /* remote error in protocol */
  18. #define EX_NOPERM       77      /* permission denied */
  19. #define EX_CONFIG       78      /* configuration error */
  20.
  21. #define EX__MAX 78      /* maximum listed value */
  使用示例
  示例一 退出当前shell
  [root@new55 ~]#
  [root@new55 ~]# exit
  logout
  示例二 在脚本中,进入脚本所在目录,否则退出
  Bash代码
  1. cd $(dirname $0) || exit 1
  示例三 在脚本中,判断参数数量,不匹配就打印使用方式,退出
  Bash代码
  1. if [ "$#" -ne "2" ]; then
  2.     echo "usage: $0  "
  3.     exit 2
  4. fi
  示例四 在脚本中,退出时删除临时文件
  Bash代码
  1. trap "rm -f tmpfile; echo Bye." EXIT
  示例五 检查上一命令的退出码
  Bash代码
  1. ./mycommand.sh
  2. EXCODE=$?
  3. if [ "$EXCODE" == "0" ]; then
  4.     echo "O.K"
  5. fi


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-553886-1-1.html 上篇帖子: Shell重定向&>file、2>&1、1>&2的区别 下篇帖子: shell判断一个变量是否为空
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表