Linux退出状态号码
命令:echo $? 可以读取上一条命令的退出码
0 命令成功执行
1 一般性未知错误
2 不适合的shell命令
126 命令不可执行
127 未找到命令
128 无效的退出参数
128+x linux信号严重错误
130 通过ctrl+c退出的
255 正常范围之外的退出状态码
案例:
状态码0
yan@yan:~/leran/shell$ ls &> /dev/null
yan@yan:~/leran/shell$ echo $?
0
yan@yan:~/leran/shell$
状态码127
yan@yan:~/leran/shell$ sdadada &> /dev/null
yan@yan:~/leran/shell$ echo $?
127
yan@yan:~/leran/shell$
状态码130
yan@yan:~/leran/shell$ ^C
yan@yan:~/leran/shell$ echo $?
130
yan@yan:~/leran/shell$
状态码126
yan@yan:~/leran/shell$ ls | grep error_code
error_code
yan@yan:~/leran/shell$ ./error_code &> /dev/null
yan@yan:~/leran/shell$ echo $?
126
yan@yan:~/leran/shell$
shell脚本指定退出码
yan@yan:~/leran/shell$ cat quit_code.sh
#!/bin/bash
PATH=/home/yan/bin:/home/yan/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
export PATH
code=999
if [ -x test1.sh ]
then
code=0
fi
exit $code
yan@yan:~/leran/shell$ ls | egrep "test1.sh|quit_code.sh"
quit_code.sh
test1.sh
yan@yan:~/leran/shell$ chmod 755 quit_code.sh
yan@yan:~/leran/shell$ ./quit_code.sh
yan@yan:~/leran/shell$ echo $?
0