如何显示使用bash set -e时失败的最后一条命令? [英] How to display last command that failed when using bash set -e?
本文介绍了如何显示使用bash set -e时失败的最后一条命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 set -e
在出现第一个错误时停止执行脚本.
I am using set -e
to stop execution of a script on first error.
问题在于,这并不能告诉我出了什么问题.
The problem is that this does not tell me what went wrong.
如何更新bash脚本,以便它向我显示最后一个失败的命令?
How can update a bash script so it will display me the last command that failed?
推荐答案
使用ERR陷阱代替 set -e
;您可以传入 $ BASH_LINENO
来获取发生错误的特定行号.我在 https://stackoverflow.com/a/185900/14122 <
Instead of set -e
, use an ERR trap; you can pass $BASH_LINENO
in to get the specific line number on which the error occurred. I provide a script taking advantage of this in my answer at https://stackoverflow.com/a/185900/14122
总结:
error() {
local sourcefile=$1
local lineno=$2
# ...logic for reporting an error at line $lineno
# of file $sourcefile goes here...
}
trap 'error "${BASH_SOURCE}" "${LINENO}"' ERR
这篇关于如何显示使用bash set -e时失败的最后一条命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文