码迷,mamicode.com
首页 > 系统相关 > 详细

linux shell 命令之trap之捕捉DEBUG信号

时间:2021-04-14 12:02:07      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:函数   成功   执行   linu   exec   bash   rap   bug   状态码   

linux 使用trap命令进行调试

Shell脚本在搪行时,会产生三个所谓的伪信号。
三种伪信号如下:
信号名称 产生条件
EXIT 从函数中退出,或者整个脚本执行完毕
ERR 当一条命令返回非零状态码,即命令执行不成功
DEBUG 脚本中的每一条命令执行之前

vi trapdebug.sh
#!/bin/bash

trap ‘echo "before execute line:$LINENO, a=$a, b=$b, c=$c"‘ DEBUG #trap命令捕捉DEBUG
a=0
b=2
c=100

while :
do
if ((a >= 10))
then
break
fi
let "a=a+2" #a、b、c值不断变化
let "b=b*2"
let "c=c-10"
done

./trapdebug.sh
before execute line:4, a=, b=, c=
before execute line:5, a=0, b=, c=
before execute line:6, a=0, b=2, c=
before execute line:8, a=0, b=2, c=100
before execute line:10, a=0, b=2, c=100
before execute line:14, a=0, b=2, c=100
before execute line:15, a=2, b=2, c=100
before execute line:16, a=2, b=4, c=100
before execute line:8, a=2, b=4, c=90
before execute line:10, a=2, b=4, c=90
before execute line:14, a=2, b=4, c=90
before execute line:15, a=4, b=4, c=90
before execute line:16, a=4, b=8, c=90
before execute line:8, a=4, b=8, c=80
before execute line:10, a=4, b=8, c=80
before execute line:14, a=4, b=8, c=80
before execute line:15, a=6, b=8, c=80
before execute line:16, a=6, b=16, c=80
before execute line:8, a=6, b=16, c=70
before execute line:10, a=6, b=16, c=70
before execute line:14, a=6, b=16, c=70
before execute line:15, a=8, b=16, c=70
before execute line:16, a=8, b=32, c=70
before execute line:8, a=8, b=32, c=60
before execute line:10, a=8, b=32, c=60
before execute line:14, a=8, b=32, c=60
before execute line:15, a=10, b=32, c=60
before execute line:16, a=10, b=64, c=60
before execute line:8, a=10, b=64, c=50
before execute line:10, a=10, b=64, c=50
before execute line:12, a=10, b=64, c=50

linux shell 命令之trap之捕捉DEBUG信号

标签:函数   成功   执行   linu   exec   bash   rap   bug   状态码   

原文地址:https://www.cnblogs.com/zhudaheng123/p/14654667.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!