本文链接: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.  */

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 */

使用示例

示例一 退出当前shell

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

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

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

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

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

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

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

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

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

问题思考

相关资料

【1】91linux    Linux exit 命令     
【2】曲径通幽   [概念]exit n     
【3】Linux大学  Bash Shell Exit Status Tutorial with Practical Examples

Linux命令之exit的更多相关文章

  1. Linux命令之exit - 退出当前shell【返回值状态】

    原文链接:http://codingstandards.iteye.com/blog/836625   (转载请注明出处) 用途说明 exit命令用于退出当前shell,在shell脚本中可以终止当前 ...

  2. Linux命令随笔

    Linux命令总结 man ==命令帮助; help ==命令的帮助(bash的内置命令); ls ==list,查看目录列表; -ld:查看目录权限; -l:(long)长格式显示属性; -F:给不 ...

  3. C语言编程实现Linux命令——who

    C语言编程实现Linux命令--who 实践分析过程 who命令是查询当前登录的每个用户,它的输出包括用户名.终端类型.登录日期及远程主机,在Linux系统中输入who命令输出如下: 我们先man一下 ...

  4. Linux 命令快捷键

    Linux 命令快捷键 tab 自动补齐(有不知道的吗)Ctrl+u 删除(剪切)此处至开始所有内容 Ctrl+k 删除从光标所在位置到行末 快速命令行 – 快捷方式• history 搜索历史执行过 ...

  5. 常见Linux命令的介绍

    今天看到了一些Linux命令的英文全称,于是,就总结上来.通过理解英文全称,去更好的理解与记忆Linux命令. 1.ls——List 列举出当前工作目录的内容(文件或文件夹),就跟你在GUI中打开一个 ...

  6. 29 个你必须知道的 Linux 命令

    虽然Linux发行版支持各种各样的饿GUI(graphical user interfaces),但在某些情况下,Linux的命令行接口(bash)仍然是简单快速的.Bash和 Linux Shell ...

  7. 学习 Linux,101: Linux 命令行

    概述 本教程将简要介绍 bash shell 的一些主要特性,涵盖以下主题: 使用命令行与 shell 和命令交互 使用有效的命令和命令序列 定义.修改.引用和导出环境变量 访问命令历史和编辑工具 调 ...

  8. Linux命令整理中...

    Linux命令整理中... 最常用命令(我最近最常用的一般放在前面tipsbychsry) clear 清屏 date 显示日期 cal 显示日历 cal 2014 显示2014年的日历 shutdo ...

  9. LINUX命令总结 -------来自 水滴娃娃 的CSDN

    LINUX命令总结 标签: LINUX命令总结 2014-01-27 15:54 41039人阅读 评论(1) 收藏 举报  分类: linux(1)  版权声明:本文为博主原创文章,未经博主允许不得 ...

随机推荐

  1. C#_串口程序_二次打包_事件响应

    using System;using System.IO.Ports;using System.Windows.Forms; namespace Dll_Serial_Comm{    public ...

  2. 【Asp.Net】后台生成控件并绑定事件

    在Asp.Net的Web页面处理流程中,有时候我们会碰到需要动态生成的控件,并为之绑定相应的事件. 接下来我们来动态的生成一个控件 //在用户代码初始化阶段添加控件 protected void Pa ...

  3. hdu 2156

    #include <iostream> #include <stdio.h> using namespace std; int main() { int i,n; while( ...

  4. python面对对象编程----1:BlackJack(21点)

    昨天读完了<Mastering Object-oriented Python>的第一部分,做一些总结. 首先,第一部分总过八章,名字叫Pythonic Classes via Specia ...

  5. pod update或者pod install很慢

    最近使用CocoaPods来添加第三方类库,无论是执行pod install还是pod update都卡在了Analyzing dependencies不动 原因在于当执行以上两个命令的时候会升级Co ...

  6. WPF自定义Main函数

    第一步:在app.xaml.cs里加入以下代码: [STAThread] public static void Main(string[] args) { App app = new App(); a ...

  7. php在.php文件中设置php.ini,仅对当前文件有效

    ini_set (PHP 4, PHP 5, PHP 7) ini_set — 为一个配置选项设置值 折叠返回值 成功时返回旧的值,失败时返回 FALSE. 折叠范例 <?php echo in ...

  8. asp.net能不托管吗?

    弱弱地问一句,整个部署在IIS中的asp.net项目能不托管吗? 或者说有没有用纯粹的非托管语言(比方说C语言)写的非托管asp.net项目?

  9. 函数式编程做用户登陆注册练习-pycharm上

    def login(username,password): """ 用户登陆 :param username: 用户名 :param password:密码 :retur ...

  10. django初探

    如果是自己建站耍的话,还是用Php方便,毕竟Php服务器便宜又到处都是. 但是python毕竟是一个新鲜的东西,特别是django,以前一直东python的语法,而且是我最早学习的语言之一,但是一直停 ...