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

后端 未结 5 1286
花落未央
花落未央 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:02

    1. make err.sh

      set -e
      trap 'echo "ERROR: $BASH_SOURCE:$LINENO $BASH_COMMAND" >&2' ERR
      
    2. include it (. err.sh) in all your scripts.

    3. replace any

      ... | while read X ; do ... ; done

      with

      while read X ; do ... ; done < <( ... )

      in your scripts for the trap to give the correct line number/command in the error message

提交回复
热议问题