基础命令学习目录首页html
原文连接:https://www.cnblogs.com/itcomputer/p/4157859.htmlshell
用途说明bash
exit命令用于退出当前shell,在shell脚本中能够终止当前脚本执行。post
格式:exit n学习
退出。设置退出码为n。(Cause the shell to exit with a status of n.)url
格式:exitspa
退出。退出码不变,即为最后一个命令的退出码。(If n is omitted, the exit status is that of the last command executed. )code
格式:$?orm
上一个命令的退出码。htm
格式: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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#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 */
|
[root@new55 ~]# [root@new55 ~]# exit logout
1
|
cd
$(
dirname
$0) ||
exit
1
|
1
2
3
4
|
if
[
"$#"
-
ne
"2"
];
then
echo
"usage: $0 <area> <hours>"
exit
2
fi
|
1
|
trap
"rm -f tmpfile; echo Bye."
EXIT
|
1
2
3
4
5
|
.
/mycommand
.sh
EXCODE=$?
if
[
"$EXCODE"
==
"0"
];
then
echo
"O.K"
fi
|
功能说明:退出目前的shell。
语 法:exit [状态值]
补充说明:执行exit可以使shell以指定的状态值退出。若不设置状态值参数,则shell以预设值退出。状态值0表明执行成功,其余值表明执行失败。exit也可用在script,离开正在执行的script,回到shell。