| 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