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

后端 未结 5 1292
花落未央
花落未央 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 04:05

    You can't use set -e by itself because processing will immediately stop after any error. Take a look at the Set Builtin section of the Bash Reference Manual for more information about the -x and -v options, which you can use for debugging.

    Something like:

    set -e
    set -v
    

    will exit on any error, while showing you each input line as it is read. It will not, however, show you just the line with the error. For that, you will need to do your own explicit error checking.

    For example:

    set +e
    if false; then
        real_exit_status=$?
        echo 'Some useful error message.' >&2
        exit $real_exit_status
    fi
    

提交回复
热议问题