I can easily redirect both standard and error output of a bash script to the file, while discarding an output to console with simple exec
statement:
exec &>>/var/log/backup.log
But how do I use exec
to write to two files, appending to first and rewriting the second? Probably some tee
magic should be used. And also some way to mute the console should be found.
The reason for this is a backup script of mine. I want to append to the main log the events of latest backup and just write only these events to the current backup log (clearing its previous content), which should be rewritten each backup session.
Best Answer
It can be done, you need process substitution. Redirect the streams into a subprocess that calls
tee
and redirects the rest into the overwritten file.Note that this will overwrite the file only once in entire script, because the stream remains open until the script exits or another redirect is established.