[comp.unix.questions] Stupid shell I/O redirection question...

dan@rna.UUCP (02/23/87)

	Forgive me for being lazy and not finding the (one true) answer myself,
but I've always wanted to know how to do the following:

	I have a shell script which issues commands that generate output on
both the stdout and stderr (1 and 2). I want to redirect both stdout and stderr
of the script through a pipe (without hacking the script (the same question
would apply to a single program that wrote on 1 and 2, anyways). Like this:

cmd:
	time date
	time who

	The time command writes its answer to stderr while date and who write
to stdout. I then wish to redirect both outputs to a pipe, say tee to both see
and capture the results (I know I could use script, etc.) The best solution I
could find is:

	(cmd 2>&1) | tee log

	I believe this wastes a shell, but just removing the () doesn't work
since the 2>&1 (dup syscall) happens before the pipe is created. Perhaps

	(exec cmd 2>&1) | tee log

	Well is there some other syntax or funny combination of >|12& to use ?

dan@rna.UUCP (02/24/87)

> 	I have a shell script which issues commands that generate output on
> both the stdout and stderr (1 and 2). I want to redirect both stdout and stderr

	To partially answer my own question, upon further experimentation, it
does appear that

	cmd 2>&1 | tee log

does do what I wanted. I was trying,

	time date 2>&1 | tee log

which doesn't work. But I think that is because "time" is built into the Korn
shell that I am using. Only

	(time date 2>&1) | tee log

works, although

	/bin/time date 2>&1 | tee log

also works.

	Which brings up the even more stupid question of how do you control
when the dup() occurs. Suppose you DID want the stderr to be the OLD stdout
and the NEW stdout to be the pipe. (why, well...)

dce@mips.UUCP (02/25/87)

File descriptors can be handled very clearly and simply by using
the fact that 'exec' with no command can be used to redirect
file descriptors in the current shell.

Let's say that you want to redirect standard error to a file and
standard out to the old standard error. This can be done by

	exec 9>&1		# fd 9 is now a duplicate of stdout
	exec 1>&2		# stdout is now a duplicate of stderr
	2>file command...	# stderr goes to 'file'
	exec 1>&9		# stdout restored to original

The idea is that you can save any file descriptor in one that isn't
being used (9 is a good candidate since it stands out), and restore
it at a later time.

The Bourne shell has some very useful features hidden in it (I still
am amazed at the prompt changing on the fly that is implemented using
traps and an infinite loop sending signals).
-- 
			David Elliott

UUCP: 	{decvax,ucbvax,ihnp4}!decwrl!mips!dce, DDD:  	408-720-1700