You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
957 B

7 years ago
exit
===
退出当前的shell
6 years ago
## 说明
7 years ago
**exit命令** 同于退出shell并返回给定值。在shell脚本中可以终止当前脚本执行。执行exit可使shell以指定的状态值退出。若不设置状态值参数则shell以预设值退出。状态值0代表执行成功其他值代表执行失败。
### 语法
```
exit(参数)
```
### 参数
返回值指定shell返回值。
### 实例
退出当前shell
```
[root@localhost ~]# 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
```