Operating System - HP-UX
1825805 Members
2168 Online
109687 Solutions
New Discussion

Re: script output to both log file and to my screen window..

 
Stuart Abramson_2
Honored Contributor

script output to both log file and to my screen window..

We have set some scripts up to log all of their output to log files, because we lost some log files in the past:

>
> LOGDIR=/var/opt/timefinder/logs
> SCRIPT=$(basename $0 .ksh)
> BCVSET=$1
> LOGDATE=$(date +%m%d%y-%H%M)
> LOG=${LOGDIR}/${SCRIPT}_${BCVSET}.${LOGDATE}.log
> exec 2>${LOG}
> exec >&2
>
> (script commands)
>


So, all of the script output goes to $LOGDIR/script_name_date.log

But, when I run the script manually (to test changes, etc), I want the output to go to my screen window (and to the log file..)..

Can I do something, in the script, like:

> exec 1>${LOG} 2>&1 tee "screen window"

Stuart
5 REPLIES 5
Steve Steel
Honored Contributor

Re: script output to both log file and to my screen window..

Hi

from man

SYNOPSIS
tee [-i] [-a] [file] ...

DESCRIPTION
The tee command transcribes the standard input to the standard output
and makes copies in the files



EXAMPLES
Write a list of users to the screen and also append the list to the
file hunt:

who | tee -a hunt

Thus
command 2>&1|tee -a $LOG

Steve Steel

If you want truly to understand something, try to change it. (Kurt Lewin)
Chris Wilshaw
Honored Contributor

Re: script output to both log file and to my screen window..

You'd need to use

exec 2>&1 | tee -a ${LOG}

This displays to screen, and appends to the log.
Leif Halvarsson_2
Honored Contributor

Re: script output to both log file and to my screen window..

Hi,

Try with removing the exec commands from the script. When you want the output to log file only:
script >logfile
To screen AND logfile:
script | tee -a logfile
Graham Cameron_1
Honored Contributor

Re: script output to both log file and to my screen window..

Stuart

Can't think of a quick way to do exactly what you ask, but how about?

tail -f $LOGDIR/script_name_date.log

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Michael Schulte zur Sur
Honored Contributor

Re: script output to both log file and to my screen window..

Hi,

my suggestion would be:
> exec 2>&1 | tee -a ${LOG}

Michael