Search code examples
bashsolaristee

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

Solution

  • 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).