Xref: utzoo comp.unix.questions:25267 comp.unix.internals:88 Path: utzoo!attcan!uunet!mcsun!i2unix!inria!ign!eh From: eh@ign.UUCP (eh@phenix pour news) Newsgroups: comp.unix.questions,comp.unix.internals Subject: temporary redirection under a Bourne-shell using "exec" Keywords: shell exec redirection Message-ID: <977@ign.UUCP> Date: 6 Sep 90 11:39:01 GMT Organization: IGN France, St Mande Lines: 47 Hi, Here is a small trick in Bourne Shell, concerning temporary redirections (and correct restoration) without entering a sub-shell. In a shell-script (Sys V.3, Bourne shell), I wanted to temporarly desactivate "stdout" without entering a sub-shell, because I assigned variables in it. The order to close stdout for the given shell is "exec >&-", but once it is closed, it is lost till the end of the shell, unless you save it in a brand new File Descriptor. This is an application of the "exec" command. Here is a short example : exec 3<&1 >&- # open File Descriptor 3 (stdout), and close stdout echo invisible # shell stdout is closed, local stdout is FD 3 exec 1<&3 3>&- # reopen stdout (and close FD 3) echo hello In this example, you assign "stdout" to FD 3 and you close FD 1 (stdout of the shell). When I run this shell-script (toto), "hello" appears. When I run toto >/dev/null, nothing appears, which proves that "stdout" is correctly restored. Of course, you can use files for temporary redirection, or devices... If you want to desactivate the echo on the console (e.g. reading a password), you can use "stty -echo" : echo "Password: \c" stty -echo read word stty echo # then crypt it and compare it to the crypted entry Yours, Eric --------------------------------------------------------------------------- Eric HURTEBIS (Institut Geographique National - France) Net: eh@ign.uucp Tel: +33 1 43 98 80 00 ext.7366 ---------------------------------------------------------------------------