12

【数据挖掘】Shell退出状态码及其应用

 3 years ago
source link: https://mp.weixin.qq.com/s?__biz=MzU4Mzg4MDU1MA%3D%3D&%3Bmid=2247487346&%3Bidx=1&%3Bsn=b7297fcc7cc07e5a992d898bcad390fb
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

NbmMR3I.gif!mobile

Shell 中运行的命令会使用0-255之间的整数值,作为退出状态码,并以此来告知shell该命令执行的状态。通常情况下,约定0代表命令成功结束,非0代表程序非正常退出。

典型退出状态码及其含义

退出状态码

含义

0

命令运行成功

1

通知未知错误

2

误用shell命令

126

命令不可执行

127

没有找到命令

128

无效退出参数

128+x

linux信号x的严重错误

130

命令通过Ctrl+C终止

255

退出状态码越界

一、退出状态码的小实验

小实验1

未指定函数返回值,且函数最后一条命令执行成功

#!/bin/bash
function hello(){
#echo "Hello World";
ech "Hello World";
return 3
# return 260
}
hello
echo $?

执行结果

****@****:/******$ bash test.sh
Hello World
0

说明:由于此时未指定返回值,所以以函数最后一条被执行的命令echo "Hello World";的执行状态作为函数的退出状态。此时 echo "Hello World";执行成功,所以返回0作为退出状态码。

小实验2

未指定函数返回值,且函数最后一条命令执行失败(以无效指令为例)

#!/bin/bash
function hello(){
#echo "Hello World";
ech "Hello World";
#return 3
# return 260
}
hello
echo $?

执行结果

****@****:/******$ bash test.sh
test.sh: line 4: ech: command not found
127

说明:此时未指定返回值,所以函数以ech "Hello World";的执行状态作为hello函数的退出状态。执行失败,且未约定特定的返回值用于标识无效指令返回值,所以此时默认以127作为退出状态返回值。

小实验3

指定函数返回值,且函数返回值在约定范围内

#!/bin/bash
function hello(){
echo "Hello World";
#ech "Hello World";
return 3
# return 260
}
hello
echo $?


运行结果

****@****:/******$ bash test.sh
Hello World
3

说明:hello函数指定了返回值为3,由于3在约定的0~255范围内,所以hello函数的退出状态值码从0变更为3(可对比小实验1)。

小实验4

指定函数返回值,且函数返回值在约定范围外

#!/bin/bash
function hello(){
echo "Hello World";
#ech "Hello World";
#return 3
return 260
}
hello
echo $?

返回结果

****@****:/******$ bash test.sh
Hello World
4

说明:hello函数指定了函数返回值为260,由于260超出了0~255,所以需要对指定的返回值进行一次取模运算,所以退出状态码由260变更为4。

小实验5

指定函数返回值,但返回值前发生命令报错

#!/bin/bash
function hello(){
#echo "Hello World";
ech "Hello World";
return 3
#return 260
}
hello
echo $?

执行结果

****@****:/******$ bash test.sh
test.sh: line 4: ech: command not found
3

说明:hello函数中一条命令执行报错并 会影响后续代码的执行 ,此时hello的函数返回值为指定的3。

小实验6

在小实验5的代码运行以后再次输入echo $?得到的还会是3么?

代码与运行结果

****@****:/******$ cat test.sh
#!/bin/bash
function hello(){
#echo "Hello World";
ech "Hello World";
return 3
#return 260
}
hello
echo $?


#exit 270




****@****:/******$ bash test.sh
test.sh: line 4: ech: command not found
3
****@****:/******$ echo $?
0

说明:由于hello函数指定了退出状态码,所以第一次输出的退出状态码为指定的整数3;第二次退出状态码则取脚本中最后一条命令echo $?命令的执行状态,由于脚本中的echo $?成功执行,所以第二次读取的退出状态码为0。

二、利用退出状态码实现命令的重试

思路:

1、 利用退出状态码,可以判断命令是否成功执行

2、 利用循环语句,可以实现命令的重试

3、 如果只设置退出状态码为0作为唯一的循环退出条件,可能出现死循环,所以最好增加最大重试次数的控制逻辑

4、$?是一个即使变化的值,如果需要多次使用,最好赋值给一个变量

代码

#! /bin/sh
count=0 #记录重试次数
while [ 0 -eq 0 ]
do
echo ".................. job begin ..................."
# date
dat
flag=$?
echo "\$?"=${flag}
if [ ${flag} -eq 0 ]; then
echo "--------------- job complete ---------------"
break;
else
count=$[ ${count}+1 ]
if [ ${count} -eq 6 ];then
echo "--------------- job failed ---------------"
break;
fi
echo "...............error occur, retry in 60 seconds,count=${count} .........."
# sleep 60
fi
done


运行结果

****@****:/******$ bash test_while.sh
.................. job begin ...................
test_while.sh: line 7: dat: command not found
$?=127
...............error occur, retry in 60 seconds,count=1 ..........
.................. job begin ...................
test_while.sh: line 7: dat: command not found
$?=127
...............error occur, retry in 60 seconds,count=2 ..........
.................. job begin ...................
test_while.sh: line 7: dat: command not found
$?=127
...............error occur, retry in 60 seconds,count=3 ..........
.................. job begin ...................
test_while.sh: line 7: dat: command not found
$?=127
...............error occur, retry in 60 seconds,count=4 ..........
.................. job begin ...................
test_while.sh: line 7: dat: command not found
$?=127
...............error occur, retry in 60 seconds,count=5 ..........
.................. job begin ...................
test_while.sh: line 7: dat: command not found
$?=127
--------------- job failed ---------------

说明: 当退出状态码非0时,代码中“ [ 0 -eq 0 ] ”和“ [ ${flag} -eq 0 ] ”部分实现了重试功能;“ [ ${count} -eq 6 ] ”部分实现了对重试逻辑不得超过6次的控制。

三、总结

Shell退出状态码:

1、 假如没有指定返回值,那么会用脚本的最后一个命令的执行状态,作为退出的状态码,支持用exit命令指定退出码。退出的状态码范围是0~255,如果自定义的退出码不在范围内,会对其执行取模运算;

2、 假如执行的是一个有返回值的函数或者程序,那么执行结束的返回值会被当做当前函数或程序的退出状态值。

往期推荐

JbAfyy7.png!mobile

qqu6f26.png!mobile

ieeyEj7.png!mobile

yiaAr2.png!mobile


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK