exit
2013-10-14 17:05
253 查看
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 信号产生
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代码
#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
C代码
#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_UN***AILABLE 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
示例二 在脚本中,进入脚本所在目录,否则退出
Bash代码
cd $(dirname $0) || exit 1
示例三 在脚本中,判断参数数量,不匹配就打印使用方式,退出
Bash代码
if [ "$#" -ne "2" ]; then
echo "usage: $0 <area> <hours>"
exit 2
fi
示例四 在脚本中,退出时删除临时文件
Bash代码
trap "rm -f tmpfile; echo Bye." EXIT
示例五 检查上一命令的退出码
Bash代码
./mycommand.sh
EXCODE=$?
if [ "$EXCODE" == "0" ]; then
echo "O.K"
fi
exit命令用于退出当前shell,在shell脚本中可以终止当前脚本执行。 常用参数格式:exit n退出。设…
用途说明
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代码
#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
C代码
#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_UN***AILABLE 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
示例二 在脚本中,进入脚本所在目录,否则退出
Bash代码 cd $(dirname $0) || exit 1
示例三 在脚本中,判断参数数量,不匹配就打印使用方式,退出
Bash代码 if [ "$#" -ne "2" ]; then
echo "usage: $0 <area> <hours>"
exit 2
fi
示例四 在脚本中,退出时删除临时文件
Bash代码 trap "rm -f tmpfile; echo Bye." EXIT
示例五 检查上一命令的退出码
Bash代码 ./mycommand.sh
EXCODE=$?
if [ "$EXCODE" == "0" ]; then
echo "O.K"
fi
相关文章推荐
- 菜鸟学Windows Phone 8开发(3)——布局和事件基础
- [搜片神器]服务器SQL2005查询分页语句你理解了么
- Oracle Process Architecture - Oracle 进程结构篇3
- iphone应用开发教程(工具为Xcode6以上,使用storyboard)
- 在linux中用窗口参看文件时,如何显示当前的路径
- sql server竟然可以有办法不让事件探测器显示具体的sql
- js中常用的正则表达式
- TI8168嵌入式系统教学实验平台 & TI8168实验箱 & TI8168教学实验箱 & TI8168DSP实验箱
- 深圳市南山区新生儿入户流程
- 自学考试-数据结构
- Android 4学习(6):概述 - 深入了解Android Activity
- My97DatePicker 没有权限问题
- GridView的七种数据绑定列的类型
- Android 4学习(6):概述 - 深入了解Android Activity
- 自学考试-数据结构
- Ubuntu 13.04 VirtualBox在工作区中的切换
- VC串口通信PurgeComm
- 算法动态规划个人总结
- dwz dialog不出来的问题
- 大整数乘法的Karatsuba算法实现