I want to trap errors with my err function, which writes to the stderr. But in this example I am getting two error lines. How can I remove the first error msg not generated by my err function? I want to prevent something like
_get_error_msg 2> /dev/null
or
exec 2> /dev/null
err() {
echo -e "[$(date +'%H:%M:%S')] \e[31mERROR\e[0m $*" >&1
}
test.sh
#!/bin/bash
set -Eeo pipefail
err() {
echo -e "[$(date +'%H:%M:%S')] \e[31mERROR\e[0m $*" >&2
}
trap 'err line "${LINENO}": "${BASH_COMMAND}"' ERR
_get_error_msg
I am getting this output:
./test.sh: line 11: _get_error_msg: command not found
[04:18:36] ERROR line 11: _get_error_msg
How can I remove
./test.sh: line 11: _get_error_msg: command not found
Or pipe this line also to my err() function to get something like this
[04:18:36] ERROR line 11: _get_error_msg: command not found
just one line written to the stderr?
EDIT: Something like this:
#!/bin/bash
set -Eeo pipefail
err() {
echo -e "[$(date +'%H:%M:%S')] \e[31mERROR\e[0m $*"
}
err_msg() {
while IFS= read -r msg; do
err "${msg}"
done
}
exec 2> >(err_msg)
trap 'err line "${LINENO}": "${BASH_COMMAND}"' ERR
_get_error_msg