Outputting to screen and log; copying file descriptors

Outputting to screen and log; copying file descriptors

Post by Patrick Van der Veke » Sat, 26 May 2001 00:04:00



Hi all,

I would like to log all messages in a script to the standard output (screen)
AND a log file. I can write a subroutine that replaces the standard 'echo'
and does two echo's instead but  I am wondering if the same can not be
achieved using extra file descriptors. If so, how can I set this up that
e.g.

1> goes to screen
2> goes to wherever
3> goes to log file

Also how could I switch the duplicate logging on and off in various places
of the script? By closing the descriptors and re-opening them afterwards. I
would appreciate some examples of this.

Kind Regards,

--
Patrick Van der Veken
Baan System Engineer & Tools Consultant
Baan Centre of Expertise - Hewlett Packard Belgium


 
 
 

Outputting to screen and log; copying file descriptors

Post by Benjamin.Altma » Sat, 26 May 2001 00:11:59



> Hi all,

> I would like to log all messages in a script to the standard output (screen)
> AND a log file. I can write a subroutine that replaces the standard 'echo'
> and does two echo's instead but  I am wondering if the same can not be
> achieved using extra file descriptors. If so, how can I set this up that
> e.g.

> 1> goes to screen
> 2> goes to wherever
> 3> goes to log file

> Also how could I switch the duplicate logging on and off in various places
> of the script? By closing the descriptors and re-opening them afterwards. I
> would appreciate some examples of this.

> Kind Regards,

> --
> Patrick Van der Veken
> Baan System Engineer & Tools Consultant
> Baan Centre of Expertise - Hewlett Packard Belgium



pipe thru tee.
e.g. echo "log message" | tee -a log.file

 
 
 

Outputting to screen and log; copying file descriptors

Post by Patrick Van der Veke » Sat, 26 May 2001 05:40:29




> > Hi all,

> > I would like to log all messages in a script to the standard output
(screen)
> > AND a log file. I can write a subroutine that replaces the standard
'echo'
> > and does two echo's instead but  I am wondering if the same can not be
> > achieved using extra file descriptors. If so, how can I set this up that
> > e.g.

> > 1> goes to screen
> > 2> goes to wherever
> > 3> goes to log file

> > Also how could I switch the duplicate logging on and off in various
places
> > of the script? By closing the descriptors and re-opening them
afterwards. I
> > would appreciate some examples of this.

> > Kind Regards,

> > --
> > Patrick Van der Veken
> > Baan System Engineer & Tools Consultant
> > Baan Centre of Expertise - Hewlett Packard Belgium


> pipe thru tee.
> e.g. echo "log message" | tee -a log.file

Mmm, I forgot about 'tee'. Yet it is too clumsy for my liking if my scripts
has 100 odd echo's in it.
Thanks for the feedback anyway!
 
 
 

Outputting to screen and log; copying file descriptors

Post by Benjamin.Altma » Sat, 26 May 2001 05:48:41






> > > Hi all,

> > > I would like to log all messages in a script to the standard output
> (screen)
> > > AND a log file. I can write a subroutine that replaces the standard
> 'echo'
> > > and does two echo's instead but  I am wondering if the same can not be
> > > achieved using extra file descriptors. If so, how can I set this up that
> > > e.g.

> > > 1> goes to screen
> > > 2> goes to wherever
> > > 3> goes to log file

> > > Also how could I switch the duplicate logging on and off in various
> places
> > > of the script? By closing the descriptors and re-opening them
> afterwards. I
> > > would appreciate some examples of this.

> > > Kind Regards,

> > > --
> > > Patrick Van der Veken
> > > Baan System Engineer & Tools Consultant
> > > Baan Centre of Expertise - Hewlett Packard Belgium


> > pipe thru tee.
> > e.g. echo "log message" | tee -a log.file

> Mmm, I forgot about 'tee'. Yet it is too clumsy for my liking if my scripts
> has 100 odd echo's in it.
> Thanks for the feedback anyway!

Then make a function that uses tee but has a simpler interface that would suit
your purposes:
    function log {

    }

SO if you would have done:
    echo "log something"
you would now do:
    log "log something"

 
 
 

Outputting to screen and log; copying file descriptors

Post by Joerg Brueh » Thu, 31 May 2001 22:31:24


Hi !





> > > Hi all,

> > > I would like to log all messages in a script to the standard output
> (screen)
> > > AND a log file. [...]

For _all_ output (not just "echo" messages, but those from called
programs as well, including "standard error"), I use

   PROT=/tmp/prot$$
   PIPE=/tmp/xyz$$
   mkfifo $PIPE
   tee $PROT < $PIPE &
   exec > $PIPE
   exec 2>&1

close to the start of my (Bourne shell) script.
From then on, every output will be duplicated. As the FIFO is now
in use, you can immediately delete it - the sequence still works:

   rm $PIPE

Quote:

> > > Also how could I switch the duplicate logging on and off in various
> places
> > > of the script? By closing the descriptors and re-opening them
> afterwards. I
> > > would appreciate some examples of this.

No idea for this, at the moment.

Quote:

> > pipe thru tee.
> > e.g. echo "log message" | tee -a log.file

> Mmm, I forgot about 'tee'. Yet it is too clumsy for my liking if my scripts
> has 100 odd echo's in it.

AFAIK, there is no standard solution for this without 'tee'.

You could probably build one using 'sed' and multiple 'w' commands,
but I never did so.

Regards,
Joerg Bruehe

--
Joerg Bruehe, SQL Datenbanksysteme GmbH, Berlin, Germany
     (speaking only for himself)