原文链接:http://codingstandards.iteye.com/blog/836625   (转载请注明出处)

用途说明

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

  1. #define EXIT_FAILURE    1       /* Failing exit status.  */
  2. #define EXIT_SUCCESS    0       /* Successful exit status.  */
#define EXIT_FAILURE    1       /* Failing exit status.  */
#define EXIT_SUCCESS 0 /* Successful exit status. */

BSD试图对退出码标准化。

man 3 exit 写道
BSD has attempted to standardize exit codes; see the file <sysexits.h>.

以下摘自/usr/include/sysexits.h

  1. #define EX_OK           0       /* successful termination */
  2. #define EX__BASE        64      /* base value for error messages */
  3. #define EX_USAGE        64      /* command line usage error */
  4. #define EX_DATAERR      65      /* data format error */
  5. #define EX_NOINPUT      66      /* cannot open input */
  6. #define EX_NOUSER       67      /* addressee unknown */
  7. #define EX_NOHOST       68      /* host name unknown */
  8. #define EX_UNAVAILABLE  69      /* service unavailable */
  9. #define EX_SOFTWARE     70      /* internal software error */
  10. #define EX_OSERR        71      /* system error (e.g., can't fork) */
  11. #define EX_OSFILE       72      /* critical OS file missing */
  12. #define EX_CANTCREAT    73      /* can't create (user) output file */
  13. #define EX_IOERR        74      /* input/output error */
  14. #define EX_TEMPFAIL     75      /* temp failure; user is invited to retry */
  15. #define EX_PROTOCOL     76      /* remote error in protocol */
  16. #define EX_NOPERM       77      /* permission denied */
  17. #define EX_CONFIG       78      /* configuration error */
  18. #define EX__MAX 78      /* maximum listed value */
#define EX_OK           0       /* successful termination */

#define EX__BASE        64      /* base value for error messages */

#define EX_USAGE        64      /* command line usage error */
#define EX_DATAERR 65 /* data format error */
#define EX_NOINPUT 66 /* cannot open input */
#define EX_NOUSER 67 /* addressee unknown */
#define EX_NOHOST 68 /* host name unknown */
#define EX_UNAVAILABLE 69 /* service unavailable */
#define EX_SOFTWARE 70 /* internal software error */
#define EX_OSERR 71 /* system error (e.g., can't fork) */
#define EX_OSFILE 72 /* critical OS file missing */
#define EX_CANTCREAT 73 /* can't create (user) output file */
#define EX_IOERR 74 /* input/output error */
#define EX_TEMPFAIL 75 /* temp failure; user is invited to retry */
#define EX_PROTOCOL 76 /* remote error in protocol */
#define EX_NOPERM 77 /* permission denied */
#define EX_CONFIG 78 /* configuration error */ #define EX__MAX 78 /* maximum listed value */

使用示例

示例一 退出当前shell

[root@new55 ~]# [root@new55 ~]# exit logout

示例二 在脚本中,进入脚本所在目录,否则退出

  1. cd $(dirname $0) || exit 1
cd $(dirname $0) || exit 1

示例三 在脚本中,判断参数数量,不匹配就打印使用方式,退出

  1. if [ "$#" -ne "2" ]; then
  2. echo "usage: $0 <area> <hours>"
  3. exit 2
  4. fi
if [ "$#" -ne "2" ]; then
echo "usage: $0 <area> <hours>"
exit 2
fi

示例四 在脚本中,退出时删除临时文件

  1. trap "rm -f tmpfile; echo Bye." EXIT
trap "rm -f tmpfile; echo Bye." EXIT

示例五 检查上一命令的退出码

  1. ./mycommand.sh
  2. EXCODE=$?
  3. if [ "$EXCODE" == "0" ]; then
  4. echo "O.K"
  5. fi
./mycommand.sh
EXCODE=$?
if [ "$EXCODE" == "0" ]; then
echo "O.K"
fi

linux之exit的更多相关文章

  1. Linux中exit与_exit的区别

    在exit,_exit的区别 - exit()与_exit()函数的区别(Linux系统中)2012-03-20 15:19:53 分类: LINUX 注:exit()就是退出,传入的参数是程序退出时 ...

  2. Linux ssh exit,启动的后台进程不会停止

    一般情况下,想要通过终端长时间运行任务,需要使用nohup 或者 screen,如果不使用会怎么样呢?来测试一下   描述: 场景1:ssh登录机器,通过添加(&),启动任务到后台,通过exi ...

  3. Linux多任务编程之四:exit()函数及其基础实验(转)

    来源:CSDN  作者:王文松   转自Linux公社 exit()和_exit()函数 函数说明 创建进程使用fork()函数,执行进程使用exec函数族,终止进程则使用exit()和_exit() ...

  4. 在Linux下运行C语言程序

    市面上常见的Linux都是发行版本,典型的Linux发行版包含了Linux内核.桌面环境和各种常用的必备工具,国内使用较多的是Ubuntu(乌班图).CentOS.Deepin(深度Linux).本教 ...

  5. 常用Linux命令小结

    常用Linux命令小结 Linux下有很多常用的很有用的命令,这种命令用的多了就熟了,对于我来说,如果长时间没有用的话,就容易忘记.当然,可以到时候用man命令查看帮助,但是,到时候查找的话未免有些临 ...

  6. Linux 进程管理剖析--转

    地址:http://www.ibm.com/developerworks/cn/linux/l-linux-process-management/index.html Linux 是一种动态系统,能够 ...

  7. linux中screen命令的用法

    http://www.9usb.net/201002/linux-screen-mingling.html 作为linux服务器管理员,经常要使用ssh登陆到远程linux机器上做一些耗时的操作.也许 ...

  8. 【原创】jssh linux scp ssh 免密登录开源工具

    项目名 JSSH git地址: https://gitee.com/chejiangyi/jssh 项目介绍 linux scp(文件上传,下载) shell命令的java ssh jar和linux ...

  9. Linux 驱动开发

    linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ...

随机推荐

  1. AngularJS最佳实践

    1.依赖注入不要用推断式 2.双向绑定的变量设置成$scope下的一个对象的属性 3.多个控制器之间的通信尽量使用service实现,不要使用全局变量或者$rootScope 4.尽量不在控制器中操作 ...

  2. ORA-04031: Unable To Allocate 32 Bytes Of Shared Memory

    记录一次生产库遇到的4031错误,后来通过调整sga大小将问题解决了 报错信息: ORA-04031: 无法分配 32 字节的共享内存 ("shared pool","s ...

  3. git提交时报错 permission denied

    git push 时报错:permission denied xxx 目前很多解决办法是生成公钥和秘钥,这种方法安全可靠,比较适用于一台电脑对应一个git账户,但是多个账户在同一台电脑上提交使用git ...

  4. MySQL-Xtrabackup备份还原

    前言 通常我们都是使用xtrabackup工具来备份数据库,它是一个专业的备份工具,先来简单介绍下它. Xtrabackup percona提供的mysql数据库备份工具,惟一开源的能够对innodb ...

  5. C盘扩容 更改C盘大小

    最近对xamarin有点兴趣,虽然网上的评论嘘声一片, 对于只想试一试的心态来说,对于网上所说的什么开发后的程序卡顿,可以用的三方库很少等, 我只想说,你们说的我不信,我要试一试看 我本来已经安装了v ...

  6. Python9-IO模型-day41

    # 进程:启动多个进程,进程之间是由操作系统负责调用# 线程:启动多个线程,真正由被cpu执行的最小单位实际是线程# 开启一个线程,创建一个线程,寄存器.堆栈# 关闭一个线程# 协程# 本质上是一个线 ...

  7. Linux命令之---mkdir

    命令简介 mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录. 命令格式 mkdir [选项] 目录...(这里可以是多个目 ...

  8. 笔记-python-standard library-17.2 multiprocessing

    笔记-python-standard library-17.2 multiprocessing 1.      multiprocessing source code:Lib/multiprocess ...

  9. Asp.net Mvc Action重定向总结

    摘自博客园 程晓晖 [HttpPost]        public ActionResult StudentList( string StudName, string studName, DateT ...

  10. 单例模式【python】

    在python中,如需让一个类只能创建一个实例对象,怎么能才能做到呢? 思路:1.通过同一个类创建的不同对象,都让他们指向同一个方向.   2.让个类只能创建唯一的实例对象. 方法:用到 _ _new ...