How to display last command that failed when using bash set -e?

后端 未结 5 1402
花落未央
花落未央 2021-02-04 03:51

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.

How can update a bash scr

5条回答
  •  没有蜡笔的小新
    2021-02-04 03:57

    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

    To summarize:

    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
    

提交回复
热议问题