Linux命令之exit - 退出当前shell【返回值状态】
用途说明
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 信号产生
以下摘自/usr/include/stdlib.h
BSD试图对退出码标准化。
以下摘自/usr/include/sysexits.h
#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
示例二 在脚本中,进入脚本所在目录,否则退出
cd $(dirname $0) || exit 1
示例三 在脚本中,判断参数数量,不匹配就打印使用方式,退出
if [ "$#" -ne "2" ]; then
echo "usage: $0 <area> <hours>"
exit 2
fi
示例四 在脚本中,退出时删除临时文件
trap "rm -f tmpfile; echo Bye." EXIT
示例五 检查上一命令的退出码
./mycommand.sh
EXCODE=$?
if [ "$EXCODE" == "0" ]; then
echo "O.K"
fi
问题思考
相关资料
【1】91linux Linux exit 命令 【2】曲径通幽 [概念]exit n 【3】Linux大学 Bash Shell Exit Status Tutorial with Practical Examples
Linux命令之exit - 退出当前shell【返回值状态】的更多相关文章
- 统计文件种类数+获取子shell返回值的其它方法
前言 只是作为一个shell的小小练习和日常统计用,瞎折腾的过程中也是摸到了获取子shell返回值的几种方法: 肯定还有别的方法,跟进程间的通信相关,希望你能提出建议和补充,谢谢~ 完整程序: #! ...
- Linux命令之exit
本文链接:http://codingstandards.iteye.com/blog/836625 (转载请注明出处) 用途说明 exit命令用于退出当前shell,在shell脚本中可以终止当前 ...
- Linux Shell 返回值之 PIPESTATUS
BASH SHELL中,通常使用 $? 来获取上一条命令的返回码,对于管道中的命令,使用$?只能获取管道中最后一条命令的返回码,例如: 下面的例子 /djdjal/dajiojidksj.file是一 ...
- Linux命令行–基本的bash shell命令
启动shell: /etc/passwd:包含系统用户账户列表以及每个用户的基本配置信息 每个条目有七个字段,每个字段用冒号隔开 用户名 用户密码 用户的系统UID 用户的系统GID 用户的全名 用户 ...
- linux shell 和linux 命令的区别?windows shell 和 windows 命令呢?
shell翻译成壳的意思,它是包裹在linux内核外层的,一个可通过一系列的linux命令对操作系统发出相关指令的人机界面. shell可以通过其条件语句和循环语句等,把一系列linux命令结合在一起 ...
- bat 获取命令执行后的多个返回值,并取最后一个
最近在使用bat,遇到了这样的问题,获取adb shell cat命令之后的所有返回值,查了很长时间,才找到,现分享给大家. 举例如下: @for /f "tokens=*" %% ...
- 常见linux命令释义(第六天)——shell环境变量
太懒了,这几天好像得了懒癌,一点都不想写博客.后来想想,知识嘛,还是分享出来的好.第一治自己的懒癌:第二顺便巩固下自己的知识. Linux的变量分为两种,一种是系统变量,是系统一经启动,就写进内存中的 ...
- Linux命令(27):shell 结合expect,多服务器批量分发数据
shell 结合expect 写的批量scp脚本工具 except安装:http://www.cnblogs.com/lovychen/p/6525623.html expect用于自动化地执行lin ...
- linux命令(26):Bash Shell 获取进程 PID
转载地址:http://weyo.me/pages/techs/linux-get-pid/ 根据pid,kill该进程:http://www.cnblogs.com/lovychen/p/54113 ...
随机推荐
- nginx 出现413 Request Entity Too Large问题的解决方法
nginx 出现413 Request Entity Too Large问题的解决方法 使用php上传图片(大小1.9M),出现 nginx: 413 Request Entity Too Large ...
- Ibatis学习总结6--使用 SQL Map API 编程
SQL Map API 力求简洁.它为程序员提供 4 种功能:配置一个 SQL Map,执行 SQL update操作,执行查询语句以取得一个对象,以及执行查询语句以取得一个对象的 List. 配置 ...
- iOS开发小技巧--设置按钮圆角
方法一:代码设置 方法二:通过图形化界面
- requirejs
//index.html <!doctype html> <html> <head> <meta charset="utf-8"> ...
- hdu2072 字典树
这题印象深刻,我刚接触acm时,以为这题是水题(因为是中文,又短),一直没做出.现再想想也是.可能也是我以前字符串掌握不好: 这题其实也可以用stl里的map写.这里我用字典树写的.其实这题算简单题了 ...
- TCP/IP详解 笔记八
UDP协议 UDP是传输层协议,提供无连接不可靠的数据传输,其优点失效率高,确定确定是无序不可靠. 报文格式 UDP头部 TCP和UDP的端口号是独立的 UDP长度是指UDP数据报的总长度 UDP的校 ...
- C/C+小记
1.struct与typedef struct struct Student{int a;int b}stu1; //定义名为Student的结构体,及一个Student变量stu1 struct { ...
- [转载]sed 简明教程
文章转载自酷壳 – CoolShell.cn,作者:陈皓,地址http://coolshell.cn/articles/9104.html awk于1977年出生,今年36岁本命年,sed比awk大2 ...
- WPF 检测管理员权限
// 检查是否是管理员身份 private static void CheckAdministrator() { WindowsIdentity wi = null; try { wi = Windo ...
- StackExchange Redis如何实现BRPOP/BLPOP
今天在使用StackExchange Redis客户端时.我想要使用BRPOP,但是我发现StackExchange Redis并没有提供API,没办法只好找资料看文档了. 原来StackExchan ...