Pipe input file redirection.

Dave Ciemiewicz ciemo at bananaPC.wpd.sgi.com
Thu Sep 27 11:08:22 AEST 1990


In article <9009262123.AA24525 at mcirps2.med.nyu.edu>,
karron at MCIRPS2.MED.NYU.EDU writes:
> 
> How do I pipe the stdout and stderr files from a collection of programs
> into the stdin of another program ?
> 
> This does not work.
> 
> #! /bin/sh
> (
> BMDstat $1 2>1
> od -d $1 2048. d
> ) | more
> 
> I am still, after all these years, mystified by sh and file numbers.
> 
> Is there a similar incantation for pipes ?
> 
> dan.

The UNIX convention for Standard I/O (stdio) is to assign input and output
streams accordingly:

	Stream	File Descriptor Number
	======	======================
	stdin	0
	stdout	1
	stderr	2

If you look in /usr/include/stdio.h, you see the following:

	#define stdin		(&_iob[0])
	#define stdout		(&_iob[1])
	#define stderr		(&_iob[2])

The first 3 file descriptors are allocated to stdio.  Nothing magical,
just not widely advertised.

Bourne shell uses the notation i>&j for merging output on stream i into
stream j.  In this case, to merge stderr into stdout, use the notation 2>&1
as such:

	BMDstat $1 2>&1


As a side note, a really useful book for UNIX is "The UNIX Programming
Environment" by Kernignan and Pike from Prentice-Hall.  They have a
discussion of stdio redirection in section "3.7 More on I/O Redirection"
on p.92.

						--- Ciemo



More information about the Comp.sys.sgi mailing list