ksh Logging

Hi,

I want to run my script and want to ensure it captures all logging.

For e.g I got this from another forum

# Redirect the current stderr into stdout
exec 2>&1
# Redirect the current stdout into the log file
exec 1>timer_log.log

The script will print stderr onto the putty terminal and log all script output into timer_log.log
How can I also print the stdout to the putty terminal too.
I am using a KSH script.

Thanks.

I was thinking about going through your commands one step after the other, assuming both stdout and stderr point to /dev/tty originally. But then I found this is completely covered in man bash :

Do you see the parallel / analogy?

I was going to say:
Do the exec 's in the other order:

exec 1>timer_log.log
exec 2>&1

or just use one exec :

exec 1>timer_log.log 2>&1

and since 1 is the default output file descriptor, one can shorten that to:

exec >timer_log.log 2>&1

but RudiC beat me to it. Note, however, that since the question is about ksh (not bash ), one should probably read the ksh man page (instead of the bash man page) where it might say something like:

But since the basic redirections are the same in ksh and bash , either man page covers this issue. However, if the question were about redirecting output to a co-process or to a specific socket on a server, one should definitely use the man page for the shell that is being used.

In addition to what RudiC already said: if you want to redirect larger parts of a script i would - simply for better readability - not shift around output streams in a way so that one depends on the other. I would set them separately and independently like this:

exec 2>/some/file
exec 1>/some/file

If you later decide to redirect one or the other to somewhere else it won't affect the other redirecction at all.

I hope this helps.

bakunin

Hi bakunin,
Sorry, but I must strongly disagree with your suggestion.

Opening these two files this way creates two file descriptors pointing at the same file with both writing data starting at position 1 and destructively overwriting each other's previously written data. For example the script:

#!/bin/ksh
exec 2>/some/file
exec 1>/some/file
echo ab
echo oops >&2
echo cd

ends up with the following stored in the file named /some/file :

oopcd
2 Likes