基础命令学习目录首页
原文链接:https://www.cnblogs.com/itcomputer/p/4157859.html
用途说明
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 2 | |
BSD试图对退出码标准化。
BSD has attempted to standardize exit codes; see the file <sysexits.h>.
以下摘自/usr/include/sysexits.h
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
使用示例
示例一 退出当前shell
[root@new55 ~]# [root@new55 ~]# exit logout
示例二 在脚本中,进入脚本所在目录,否则退出
Bash代码
|
示例三 在脚本中,判断参数数量,不匹配就打印使用方式,退出
|
示例四 在脚本中,退出时删除临时文件
|
示例五 检查上一命令的退出码
|
功能说明:退出目前的shell。
语 法:exit [状态值]
补充说明:执行exit可使shell以指定的状态值退出。若不设置状态值参数,则shell以预设值退出。状态值0代表执行成功,其他值代表执行失败。exit也可用在script,离开正在执行的script,回到shell。