randombits
randombits

Reputation: 48490

Using tee in a bash script (on Solaris)

I'm trying to build a simple log file along with a running script that I've hacked together. The lines in question look like the following:

TEE="/usr/bin/tee"
TO_LOG="/usr/bin/tee >> $LOG_DIR/hosts.`date +%Y%m%d-%H%M%S`.log"

Then those two get used like this:

echo "something happened!" | $TO_LOG

Unfortunately the only thing this is doing is creating an empty filed literally named >> in my CWD. Running this script with bash -x shows the redirect with append operator getting single quoted - although I'm not sure if that's literally happening or if that's just being appended by bash in interactive debug mode.

+ echo 'something happened!'
+ /usr/bin/tee '>>' /backups/logs/mylog.20110801-182337.log

Upvotes: 2

Views: 3430

Answers (2)

pyroscope
pyroscope

Reputation: 4158

This should be what you want...

TEE="/usr/bin/tee"
LOG="$LOG_DIR/hosts.$(date +'%Y%m%d-%H%M%S').log"
to_log() {
    $TEE -a $LOG
}

echo "test" | to_log

Upvotes: 1

Jeremy Roman
Jeremy Roman

Reputation: 16355

tee itself is intended to do the file writing. I think you want:

/usr/bin/tee -a $LOG_DIR/hosts.`date +%Y%m%d-%H%M%S`.log

The -a option causes tee to append, rather than truncate, the file (similar to how >> appends whereas > truncates in most shells).

Upvotes: 5

Related Questions