/bin/sh redirection of stdin, stderr woes

Ian Watson ian at hpopd.HP.COM
Mon Nov 19 23:02:25 AEST 1990


I want to have a Bourne shell script that does something like :

cmd >$sout 2>$serr

Under certain conditions, sout and serr may be set earlier in the script to
given files, otherwise the stdin and stderr from the calling process are to
be inherited.  The simple way of doing this is to have something like :

if vars_were_set_in_script ; then
	cmd >$sout 2>$serr
else
	cmd
fi

Doing this everywhere is verbose and horrible.  Next attempt is to leave sout
and serr set to null so that the command

cmd >$sout 2>$serr

is effectively 

cmd > 2>

Now, the questions :  

(1) Are there any pitfalls with this method -- redirecting to nowhere ?

(2) Is there any way of making the intention more clear ?  I have been trying
to get sout and serr set in such a way that the effective command is

cmd >&1 2>&2

without success (syntax errors, '&' being evaluated as background
process indicator, creation of files "&1" and "&2" etc.).

Thanks in advance,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ian Watson, HP Pinewood Information Systems Division, England.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Phone :				(Intl)+44 344 763015
Unix mail (Internet) :		ian at hpopd.HP.COM
Unix mail (UUCP) :		...!hplabs!hpopd!ian
Openmail :			ian watson/pinewood,lab,hpopd
Openmail from Unix :		ian_watson/pinewood_lab_hpopd at hpopd
HPDesk :			Ian WATSON/HP1600
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



More information about the Comp.unix.shell mailing list