Shell退出状态码及其应用详解

(编辑:jimmy 日期: 2024/9/21 浏览:2)

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 $"mailto:****@****:/******$" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >****@****:/******$ 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 $"mailto:****@****:/******$" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >****@****:/******$ 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 $"mailto:****@****:/******$" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >****@****:/******$ 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 $"mailto:****@****:/******$" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >****@****:/******$ 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 $"mailto:****@****:/******$" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >****@****:/******$ bash test.sh
test.sh: line 4: ech: command not found
3

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

小实验6

在小实验5的代码运行以后再次输入echo $"htmlcode">

****@****:/******$ cat test.sh
#!/bin/bash
function hello(){
  #echo "Hello World";
  ech "Hello World";
  return 3
  #return 260
}
hello
echo $"htmlcode">
#! /bin/sh
count=0   #记录重试次数
while [ 0 -eq 0 ]
do
  echo ".................. job begin ..................."
#  date
  dat
  flag=$"\$"=${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、 假如执行的是一个有返回值的函数或者程序,那么执行结束的返回值会被当做当前函数或程序的退出状态值。

一句话新闻

微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。