[comp.unix.shell] /bin/sh redirection of stdin, stderr woes

ian@hpopd.HP.COM (Ian Watson) (11/19/90)

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@hpopd.HP.COM
Unix mail (UUCP) :		...!hplabs!hpopd!ian
Openmail :			ian watson/pinewood,lab,hpopd
Openmail from Unix :		ian_watson/pinewood_lab_hpopd@hpopd
HPDesk :			Ian WATSON/HP1600
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

maart@cs.vu.nl (Maarten Litmaath) (11/21/90)

In article <28770001@hpopd.HP.COM>,
	ian@hpopd.HP.COM (Ian Watson) writes:
)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

Try this:

	(
		test $vars_are_set && exec > $sout 2> $serr
		cmd
	)

Alternatively:

	if some_condition
	then
		OUT="> $outputfile"
		ERR="2> $errorfile"
	fi
	...
	eval "cmd $OUT $ERR"
--
"Please DON'T BREAK THE CHAIN!  Terry Wood broke the chain and ended up
writing COBOL PROGRAMS.  Three days later, he found his Blue Star Tatoo
Letter, made 20 copies and mailed them out.  He found a good job writing
compilers."  --  tjw@unix.cis.pitt.edu (Terry J. Wood)

frank@grep.co.uk (Frank Wales) (11/23/90)

In article <28770001@hpopd.HP.COM> ian@hpopd.HP.COM (Ian Watson) writes:
>I want to have a Bourne shell script that does something like :
>cmd >$sout 2>$serr

Here is a short script which demonstrates a technique you could try:

#!/bin/sh -
cmd=cat					# example command

# sample target set-up
case $# in
0) stdout="&1"; stderr="&2";;		# default both
1) stdout="$1"; stderr="&2";;		# specify stdout, default stderr
2) stdout="$1"; stderr="$2";;		# specify both
*) echo usage: $0 [stdout [stderr]] >&2; exit 1;;
esac

# command evaluation
evalarg="$cmd >$stdout 2>$stderr"	# build the command line
eval $evalarg				# do it
--
Frank Wales, Grep Limited,             [frank@grep.co.uk<->uunet!grep!frank]
Kirkfields Business Centre, Kirk Lane, LEEDS, UK, LS19 7LX. (+44) 532 500303