[comp.sys.sgi] Pipe input file redirection.

karron@MCIRPS2.MED.NYU.EDU (09/27/90)

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.
+-----------------------------------------------------------------------------+
| karron@nyu.edu                          Dan Karron                          |
| . . . . . . . . . . . . . .             New York University Medical Center  |
| 560 First Avenue           \ \    Pager <1> (212) 397 9330                  |
| New York, New York 10016    \**\        <2> 10896   <3> <your-number-here>  |
| (212) 340 5210               \**\__________________________________________ |
+-----------------------------------------------------------------------------+

ciemo@bananaPC.wpd.sgi.com (Dave Ciemiewicz) (09/27/90)

In article <9009262123.AA24525@mcirps2.med.nyu.edu>,
karron@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

merritt@iris613.gsfc.nasa.gov (John H Merritt) (09/27/90)

In article <9009262123.AA24525@mcirps2.med.nyu.edu> karron%CMCL2.NYU.EDU@cunyvm.cuny.edu writes:
>
>How do I pipe the stdout and stderr files from a collection of programs
>into the stdin of another program ?
>
[ example deleted ]

Assume no_file1 and no_file2 don't exist; 'cat' writes to 2.

With sh:
(cat no_file1; cat no_file2) 2>&1 | 1<&2 od -xc

With csh:
(cat no_file1; cat no_file2) |& od -xc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
John H. Merritt                   #  Yesterday I knew nothing,
Applied Research Corporation      #  Today I know that.
merritt@iris613.gsfc.nasa.gov     #

moss@brl.mil (Gary S. Moss (VLD/VMB) <moss>) (10/01/90)

In article <3502@dftsrv.gsfc.nasa.gov>, merritt@iris613.gsfc.nasa.gov (John H Merritt) writes:
|> In article <9009262123.AA24525@mcirps2.med.nyu.edu> karron%CMCL2.NYU.EDU@cunyvm.cuny.edu writes:
|> >How do I pipe the stdout and stderr files from a collection of programs
|> >into the stdin of another program ?
|> 
|> Assume no_file1 and no_file2 don't exist; 'cat' writes to 2.
|> 
|> With sh:
|> (cat no_file1; cat no_file2) 2>&1 | 1<&2 od -xc

Well, I'm, not sure why, but this results in the output from "od" being on
stderr; when reading TFM about the "1<&2" construct it sheds no light on
reality.

Anyway, the following is sufficient to dup stderr from the subshell to its
stdout for the Bourne shell:

(cat no_file1; cat no_file2) 2>&1 | od -xc