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 ...
随机推荐
- Ibatis中传List参数
Ibatis中用list传参数的方式. Java代码 select count(id) from `user` where id in #[]# and status=1 . <select ...
- OC基础--ARC的基本使用
一.ARC的判断准则:只要没有强指针指向对象,就会释放对象 二.ARC特点: 1>不允许使用release.retain.retainCount 2>允许重写dealloc,但是不允许调用 ...
- hdu1231 最大连续子序列
#include<stdio.h> #include<string.h> #define maxn 10010 int a[maxn],dp[maxn]; int main() ...
- sprintf、strcpy和memcpy的区别
做某题用到了sprintf把一个字符数组(字符串)写到二维字符数组里,然后耗时挺长的,想了想strcpy好像也可以,事实证明strcpy效率果然更高,然后想了想觉得memcpy好像也可以.实践了一下的 ...
- 畅所欲言第1期 - 从Viola&Jones的人脸检测说起
转载自http://c.blog.sina.com.cn/profile.php?blogid=ab0aa22c890006v0 不少人认识我或者听说我的名字都是因为我过去做的关于人脸检测的工作,那么 ...
- 【poj1090】 Chain
http://poj.org/problem?id=1090 (题目链接) 题意 给出九连环的初始状态,要求将环全部取下需要走多少步. Solution 格雷码:神犇博客 当然递推也可以做. 代码 / ...
- codeforces 86D : Powerful array
Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...
- android 开发问题:java.lang.ClassCastException
java.lang.ClassCastException: libcore.net.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl ...
- mysql 高级查询
高级查询:1.连接查询select * from Info,Nation #这是两个表名,中间用逗号隔开形成笛卡尔积select * from Info,Nation where Info.natio ...
- linux ftp命令(转)
此命令需要安装ftp, yum install ftp 1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 ...