Can direct output to a pipe, leaving stdin alone?

Can direct output to a pipe, leaving stdin alone?

Post by Tim Ottinge » Tue, 27 Aug 1996 04:00:00



I know this sounds funny, but I really want to do the equivalent of a
popen
in bourne shell, but without redirecting standard out or standard error.

This should be easy:

while read this
do
   print "Okay"
   print "piped out $this" >&3
done &3>| awk '{ print "pipe 3 output is " $0 ; }'

If this worked, the loop would read from standard in (0), echo "okay" to
standard output (1), and echo the words "piped out" followed by the
input
string to an awk program.

Input could be:
    drop dead fred
    princess bride

and standard output would be:
    Okay
    pipe 3 output is piped out drop dead fred
    Okay
    pipe 3 output is piped out princess bride

What is the right way to do this?  I'm not sure how to direct anything
other than standard output to a process via "|", and the 'exec' trick
didn't seem to work either...

--
Tim
+----------------------------------------------------------------------------+

| CSC Communications Industry Services                     217-351-8508x2420 |
| TRIS Division -- Cellular Billing and Support             Fax
217-351-2640 |
+----------------------------------------------------------------------------+
|   A better metaphor [for debugging] might be crime.  It is hard for
a      |
|   detective to predict accurately how long it will take to find out
who    |
|   committed a crime.  It is harder still to prevent future crimes.
And     |
|   we've been trying to solve those problems for much
longer...             |
|                                  Bill Ehrich, via electronic
transmission  |
+----------------------------------------------------------------------------+

 
 
 

Can direct output to a pipe, leaving stdin alone?

Post by Andru Luvi » Tue, 27 Aug 1996 04:00:00


: I know this sounds funny, but I really want to do the equivalent of a
: popen
: in bourne shell, but without redirecting standard out or standard error.

you've gotta store the orig stdout/stderr descriptors.

: This should be easy:

: while read this
: do
:    print "Okay"
:    print "piped out $this" >&3
: done &3>| awk '{ print "pipe 3 output is " $0 ; }'

try this:

exec 3>&1 4>&2

while read this
do
 print "Okay" 1>&3 2>&4
 print "piped out $this"
done |awk '{print "pipe 3 output is " $0; }'

exec 3>&- 4 >&-

andru