Howto cancel I/O redirection ?

Hi

on AIX systems (6.x and 7.x) I have ksh scripts redirecting I/O, and running another script script000.ksh ie :

# my script
...
>${LOG}
>${LOGCTRL}

exec >>${LOG} 2>>${LOG}

. ${PROJECT}/.../script000.ksh

# hereafter, restore default I/O
...

Is it possible at the end of the script to restore I/O to their normal state, not feeding ${LOG} file ?

Yes. Before the redirections performed by the exec statement, save (dup) the file descriptors so that you can restore them. An example:

exec 5>&1 6>&2        # save the original stdout and stderr
exec >>${LOG} 2>&1    # redirect stdout and stderr to ${LOG}
... <do some work here> ...
exec 1>&5 2>&6        # restore the original destinations of stdout and stderr
exec 5>&- 6>&-        # close the no longer needed backup descriptors

I chose 5 and 6 for no particular reason. Naturally, if the code that does the intervening work manipulates or redirects the chosen backup descriptors, bad things could happen (including not being able to restore stdout and stderr to their initial states). Choose yours appropriately.

Regards,
Alister

1 Like

I've never had to use file descriptors in the past. Today, i've learned something very useful.
Thank You Alister, it works perfectly :b::slight_smile: