Reputation: 26046
I would like to log all error messages that the commands in a Bash script contains.
The problem is that if I have to add
E=$( ... 2>&1 ); echo $E >> $LOG
to all commands, then the script will become quite hard to read.
Question
Is it somehow possible to get a global variable, so all STDERR
becomes STDOUT
?
Upvotes: 1
Views: 1091
Reputation: 212654
You can do things like:
#!/bin/sh
test -z "$DOLOGGING" && { DOLOGGING=no exec $0 "${@}" 2>&1 | tee log-file; exit; }
...
to duplicate all output/errors to log-file. Although it seems I misread the question and it seems you just want to add exec 2>&1 >/dev/null
to the top of your script to print all errors to stdout and discard all output.
Upvotes: 0