I have a bash script "backup.sh", which prints out everything to stdout, including all the files that are transferred during backup.
I'd like to make it possible to keep that full output, but when it's called with
./backup.sh 2>stderr.log
, then only a few important lines from stdout should be copied into stderr.log.
My script currently uses the following function to print out these important lines:
errEcho() {
echo "$1"
echo "$1" 1>&2
}
echo "Some info message"
errEcho "Very important message"
But now, if somebody calls the script simply as
./backup.sh
Then the errEcho lines appear duplicated on stdout.
What's the best way to solve this? (Effectively, I'd like my script to recognize, if stdout == stderr, and then skip output to stderr, but I'm not sure, if I need a very different approach maybe).
Please note: I don't want to have two scripts, where one of them calls "backup.sh 2>stderr.log".

errEcho() { echo "$@" >> /var/log/backup.log ... }to append to a separate log file. – Chris Lercher Jul 10 '11 at 13:41