/bin/sh redirection of stdin, stderr woes

Frank Wales frank at grep.co.uk
Sat Nov 24 00:13:08 AEST 1990


In article <28770001 at hpopd.HP.COM> ian at 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 at grep.co.uk<->uunet!grep!frank]
Kirkfields Business Centre, Kirk Lane, LEEDS, UK, LS19 7LX. (+44) 532 500303



More information about the Comp.unix.shell mailing list