🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.unix.shell
3 messages
3 total messages Started by Ian Watson Mon, 19 Nov 1990 12:02
/bin/sh redirection of stdin, stderr woes
#1804
Author: Ian Watson
Date: Mon, 19 Nov 1990 12:02
46 lines
1854 bytes
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Re: /bin/sh redirection of stdin, stderr woes
#1807
Author: Maarten Litmaath
Date: Tue, 20 Nov 1990 22:58
37 lines
1493 bytes
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)
Re: /bin/sh redirection of stdin, stderr woes
#1822
Author: Frank Wales
Date: Fri, 23 Nov 1990 13:13
23 lines
1317 bytes
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
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads