I have a series of BASH shell scripts that run together. I would like it so that these scripts construct a log file as they run. This log file would ideally be a text file that contains exactly (including blank lines) what is output to the terminal.
What is the best way to accomplish this? My initial assumption is that I will use the "tee" command, ie:
echo 'Start Processing' tee -a 'log.txt'
However I wonder if this is the most efficient thing to do, as I will be using that command A LOT? Does anyone have any suggestions?
Another way for simple cases is to enclose the script (or parts of it) in curly brackets and direct the output from that to a file:
TestScript.sh
===========
#!/bin/bash
{
function TestFunc {
echo "$(date): $0: $@"
}
TestFunc this is
TestFunc just
TestFunc a test
} | tee MyLog.txt
cat MyLog.txt
Fri Aug 14 11:19:54 UTC 2009: TestFunc: this is
Fri Aug 14 11:19:54 UTC 2009: TestFunc: just
Fri Aug 14 11:19:54 UTC 2009: TestFunc: a test
You can apply the same on the command-line
{
date
sleep 1
date
sleep 1
date
} | tee dates.txt
Fri Aug 14 11:27:46 UTC 2009
Fri Aug 14 11:27:47 UTC 2009
Fri Aug 14 11:27:48 UTC 2009
cat dates.txt
Fri Aug 14 11:27:46 UTC 2009
Fri Aug 14 11:27:47 UTC 2009
Fri Aug 14 11:27:48 UTC 2009