Operating System - HP-UX
1823945 Members
3430 Online
109667 Solutions
New Discussion юеВ

ksh: How do I turn "exec >file" redirect off?

 
SOLVED
Go to solution
Stuart Abramson_2
Honored Contributor

ksh: How do I turn "exec >file" redirect off?

We have scripts which capture log files by including these statements in the beginning of the script:

...exec 2>${LOG}
...exec >&2

How do I "turn this off", so that I can write messages to "stdout" without them being logged and so I can see them when the script runs..
8 REPLIES 8
curt larson_1
Honored Contributor

Re: ksh: How do I turn "exec >file" redirect off?

one way would be to comment the lines out

#..exec 2>${LOG}
#..exec >&2
then stdout and stderr will go to where they were directed prior to the exec, hopefully that will be your terminal

or you could try something like this

exec 2>&1
tee -a /${LOG} >/dev/console |&
exec 1>&p

which will send everything to your log and to the system console.

or a little more complex

tty=$(tty)
exec 2>&1
tee -a /${LOG} >$tty |&
exec 1>&p

which hopefully will send everything to the log and to your termial
Charlie Rubeor
Frequent Advisor

Re: ksh: How do I turn "exec >file" redirect off?

You should be able to comment these lines out. After that, both stderr and stdout should show up on your display. If you comment out only the second one, then stderr should continue to the log file.
Stuart Abramson_2
Honored Contributor

Re: ksh: How do I turn "exec >file" redirect off?

Please forgive me. I did't explain myself well.

At the end of the script, after 1000 lines have been sent to the $LOG, I then want to "disable" logging to $LOG, and write "Ending 'scriptname'." on "stdout".

How do I disable the redirect at the end of the script just to squeeze out one line to "stdout".
Laurent Menase
Honored Contributor

Re: ksh: How do I turn "exec >file" redirect off?

exec 4>&2 5>&1 # backup the filedescr of 1 and 2

exec 2>$LOG >&2

....
echo to the log file
....

exec 2>&4 >&5 4>&- 5>&-

echo to the stdout


It is better than use of /dev/tty because it really restore the real output
curt larson_1
Honored Contributor

Re: ksh: How do I turn "exec >file" redirect off?

or as a variant of Laurent's suggestion:

make a backup of the file descriptors
exec 4>&2 5>&1 # backup the filedescr of 1 and 2

exec 2>$LOG >&2

then when you want to print to your terminal

print -u5 "this is what I want to see on the screen"
Tony Contratto
Respected Contributor
Solution

Re: ksh: How do I turn "exec >file" redirect off?

Stuart,

Another method. You could create a "copy" of the original stdout to a different file descriptor, then print you messages to it.

# create copy of stdout on descriptor 5
exec 5>&1

# change stdout to go to the log
exec 1>${LOG}

# print text to your term
print -u5 "some text to the screen"


That will allow the normal output from other commands in your script to go to the log, yet allow you to print stuff to the screen by using "print -u5".

--
Tony
got root?
Laurent Menase
Honored Contributor

Re: ksh: How do I turn "exec >file" redirect off?

yes you can use
echo ... >&5

the advantage of dupping back the 4 and 5 to 2 and 1 is that it closes the log file.
Stuart Abramson_2
Honored Contributor

Re: ksh: How do I turn "exec >file" redirect off?

I'm going to try all of these things tomorrow and post points!

Stuart