Redirect all messages to log file from inside script except some print command output on console as well as screen

Viewed 55

I want to write all stdout of script to a log file by default but want to display some messages on the console as well. Below is the sample code I tried.

#!/bin/bash
LOG='output.txt'
exec 3>&1
exec 1>>$LOG
exec 2>>$LOG
echo "This should be written to log"
echo "This should be written to both screen as well as log"|tee -a >&3

This writes This should be written to log to the log file, which is good.

It writes This should be written to both screen as well as log only on the console, but I want it to be written to both console and log.

1 Answers

You're missing a filename on your tee command. Try it like this:

echo "This should be written to both screen as well as log"|tee -a $LOG >&3
Related