SYSOUT & ERROR Logging in Korn Shell - Help Needed!

SYSOUT & ERROR Logging in Korn Shell - Help Needed!

Post by Jeff P. Dobi » Fri, 11 Jul 1997 04:00:00



I'm have the following problem that I need a resolution to.  If anyone
can help out I would greatly appreciate it.

Problem:  I need to make a Korn Shell function that will log the run
               time output to a file PLUS the screen.  Also, I need
               it to log all the errors to a separate error log file
               PLUS the screen AND the run time output file.

I'm aware of the exec command and file descriptors but I only know how
to get them to redirect the output to just the file or the screen and
not both.  (Ex:  exec 1>sysout.log   -> for system output
                        exec 2>error.log      -> for error messages)

Any help or assistance would be great!

Jeff Dobies

 
 
 

SYSOUT & ERROR Logging in Korn Shell - Help Needed!

Post by Donn Ca » Fri, 11 Jul 1997 04:00:00



| I'm have the following problem that I need a resolution to.  If anyone
| can help out I would greatly appreciate it.
|
| Problem:  I need to make a Korn Shell function that will log the run
|                time output to a file PLUS the screen.  Also, I need
|                it to log all the errors to a separate error log file
|                PLUS the screen AND the run time output file.

This isn't strictly a Korn shell issue, any Bourne class shell can do
this.  You're talking about copying output, and the solution to that
is a UNIX command tee(1).  The difficult part is that you want 3 copies
of the diagnostic output.

Here's how I did it - there could be other solutions.  Units 1 and 2 are
output and error output, respectively.  Unit 3 is created here for my
convenience.  The notation 2>&1 means redirect 2 into the stream already
open on 1.  Pipes involve unit 1.

$ (ls -li nosuchfile .kshrc 2>&1 1>&3 | tee /tmp/eele) 3>&1 | tee /tmp/eelo
nosuchfile not found
35208 -rw-r--r--   1 donn     user        1579 May  6 13:29 .kshrc
$ cat /tmp/eelo
nosuchfile not found
35208 -rw-r--r--   1 donn     user        1579 May  6 13:29 .kshrc
$ cat /tmp/eele
nosuchfile not found

To analyze this, start at the "outermost" level, the pipe to the final
tee.  It's copying 3 and 1 into the output log, and to the screen.
The subshell pipe is copying only 2 into the error log, because I
redirected 1 to 3, bypassing the pipe - the output from that tee is
unit 1 for the outer pipe, the output from ls is unit 3.

If you really need to set these units up during execution, as you'd
do with "exec", that's so much funkier that it would really be worth
looking at design alternatives.  You could use co-processes, first
the output tee and then the error tee, subsequently redirecting the
appropriate descriptor into the coprocess, like 2>&p.  Use the same
3>&1 technique to force output from the tees, which would normally
go back through the coprocess pipe pair.  For a more general approach
(coprocesses are ksh-specific), you could use named pipes (man mknod).

        Donn Cave, University Computing Services, University of Washington


 
 
 

SYSOUT & ERROR Logging in Korn Shell - Help Needed!

Post by Jeff P. Dobi » Fri, 11 Jul 1997 04:00:00




>| I'm have the following problem that I need a resolution to.  If anyone
>| can help out I would greatly appreciate it.
>|
>| Problem:  I need to make a Korn Shell function that will log the run
>|                time output to a file PLUS the screen.  Also, I need
>|                it to log all the errors to a separate error log file
>|                PLUS the screen AND the run time output file.

>This isn't strictly a Korn shell issue, any Bourne class shell can do
>this.  You're talking about copying output, and the solution to that
>is a UNIX command tee(1).  The difficult part is that you want 3 copies
>of the diagnostic output.

>Here's how I did it - there could be other solutions.  Units 1 and 2 are
>output and error output, respectively.  Unit 3 is created here for my
>convenience.  The notation 2>&1 means redirect 2 into the stream already
>open on 1.  Pipes involve unit 1.

>$ (ls -li nosuchfile .kshrc 2>&1 1>&3 | tee /tmp/eele) 3>&1 | tee /tmp/eelo
>nosuchfile not found
>35208 -rw-r--r--   1 donn     user        1579 May  6 13:29 .kshrc
>$ cat /tmp/eelo
>nosuchfile not found
>35208 -rw-r--r--   1 donn     user        1579 May  6 13:29 .kshrc
>$ cat /tmp/eele
>nosuchfile not found

>To analyze this, start at the "outermost" level, the pipe to the final
>tee.  It's copying 3 and 1 into the output log, and to the screen.
>The subshell pipe is copying only 2 into the error log, because I
>redirected 1 to 3, bypassing the pipe - the output from that tee is
>unit 1 for the outer pipe, the output from ls is unit 3.

>If you really need to set these units up during execution, as you'd
>do with "exec", that's so much funkier that it would really be worth
>looking at design alternatives.  You could use co-processes, first
>the output tee and then the error tee, subsequently redirecting the
>appropriate descriptor into the coprocess, like 2>&p.  Use the same
>3>&1 technique to force output from the tees, which would normally
>go back through the coprocess pipe pair.  For a more general approach
>(coprocesses are ksh-specific), you could use named pipes (man mknod).

>    Donn Cave, University Computing Services, University of Washington


Thanks for the help but is there anyway I can submit several commands
and have the processes above work correctly (using a single process).

Example Korn Shell Script:

more nosuchfile
more .kshrc
ls -las

Is there any commands that I can stick before these commands to
capture the desired results?  Or is there a way to enclose the series
of commands so that it will be logged as described originally?  Again,
thanks for your help...worse case senario, I'll have to have an extra
script for each execution - 1 for the logging process and the 2nd for
the actual execution of the script.  Obviously, it would be great if I
could reduce this down to one process/script.  

Jeff P. Dobies

 
 
 

SYSOUT & ERROR Logging in Korn Shell - Help Needed!

Post by Dan A. Merc » Fri, 11 Jul 1997 04:00:00



: I'm have the following problem that I need a resolution to.  If anyone
: can help out I would greatly appreciate it.

: Problem:  I need to make a Korn Shell function that will log the run
:                time output to a file PLUS the screen.  Also, I need
:                it to log all the errors to a separate error log file
:                PLUS the screen AND the run time output file.

: I'm aware of the exec command and file descriptors but I only know how
: to get them to redirect the output to just the file or the screen and
: not both.  (Ex:  exec 1>sysout.log   -> for system output
:                         exec 2>error.log      -> for error messages)

: Any help or assistance would be great!

: Jeff Dobies

You use a coprocess piping the output to tee:

tee -a $logfile  >/dev/tty |&     # start the coprocess
exec 2>&p                         # dup stderr as input to coprocess

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.

 
 
 

1. Need HELP to Log User Log-ins form the internet

Hi,

I've setup a FreeBSD 4.1.1-STABLE box to connect a network to the internet
with natd and ipfw firewall.
I've also setup the FreeBSD box to let teleworkers log in with FTP and
telnet.

Now I would like to log FTP and telnet Log-in's from teleworkers who connect
to the machine from the internet.
I woul like to see the time and IP numer from which users Log-in.

I have looked at the /var/log/messages file but this only shows SU Login's.

All help is greatly appriciated!

Luke

2. IPX file server on Solaris 2.5?

3. Korn shell help with remote shell needed!

4. IBM DJNA-371800

5. best korn shell resources and is there a korn shell faq

6. Linux JE HOWTO (part 1/1)

7. Trying to run nohup from Korn shell but getting Bourne shell errors

8. TCP/IP broken socket question

9. Help: Korn Shell & egrep!

10. Help w/awk, korn shell & writing to a file

11. I need help in Korn Shell

12. need help with a chmod script in korn shell

13. Need help wit korn shell syntax