[comp.unix.questions] Bourne to read?

mosemann@sardion.unl.edu (Russell Mosemann) (03/01/91)

   In the Bourne shell, why does this not work

      echo words | read W

   but this does (if I put the words in the file)

      read W < file

   ?
--
Russell Mosemann                  Internet:      mosemann@crcvms.unl.edu
Network Analyst                   Bitnet:               mosemann@unlvax1
Computing Resource Center         UUCP:   ..!uunet!hoss.unl.edu!mosemann
University of Nebraska - Lincoln  Voice:  (402) 472-5930   Fax: 472-5280

pfalstad@phoenix.Princeton.EDU (Paul Falstad) (03/01/91)

mosemann@sardion.unl.edu (Russell Mosemann) wrote:
>   In the Bourne shell, why does this not work
>      echo words | read W
>   but this does (if I put the words in the file)
>      read W < file

This is in the FAQ, although somewhat buried.  Basically, the pipe
forces read into a subshell in the first case.  sh spawns a subshell to
simplify file descriptor handling for the pipe; the subshell then reads
its input into the variable W, and then exits.  The parent shell's W is
not changed.  In the second (simpler) case, read is executed in the main
shell.  In some versions of sh (evidently not in SunOS), as well as in
ksh (I think) and in another shell whose name escapes me at the moment,
"echo words | read W" will return the expected result.

I don't know of any nice way around this.  If you can, redirect the
output to a file, and then do "read W <file".  If not, you could do
something gross like create a named pipe, etc.

--
Paul Falstad, pfalstad@phoenix.princeton.edu PLink:HYPNOS GEnie:P.FALSTAD
How DO you delete a file called "-"?  For viewers at home, the answer is
coming up on your screen.  For those of you who wish to play it the hard
way, stand upside down with your head in a bucket of piranha fish.

mike (03/01/91)

In an article, sardion.unl.edu!mosemann (Russell Mosemann) writes:
>
>   In the Bourne shell, why does this not work
>
>      echo words | read W
>
>   but this does (if I put the words in the file)
>
>      read W < file

The read is in a different instance of the shell.  The shell forks the
read, and proceeds to create/modify the environment variable.  When the
child dies, so does its environment.

-- 
Michael Stefanik, MGI Inc., Los Angeles| Opinions stated are not even my own.
Title of the week: Systems Engineer    | UUCP: ...!uunet!bria!mike
-------------------------------------------------------------------------------
Remember folks: If you can't flame MS-DOS, then what _can_ you flame?

sadkins@oucsace.cs.OHIOU.EDU (Scott W. Adkins) (03/01/91)

In article <1991Feb28.160948.24987@hoss.unl.edu> mosemann@sardion.unl.edu (Russell Mosemann) writes:
>
>   In the Bourne shell, why does this not work
>
>      echo words | read W
>
>   but this does (if I put the words in the file)
>
>      read W < file

Well, it could be a matter of where things are coming from and where they are
going to.  In the first case, echo words is sending the stuff to stdout, not
stdin.  The read W is waiting for input from stdin, not stdout.  Result?  The
words will be echoed to the screen and then the script will wait for keyboard
input.

In the second example, the '<' symbol literally means redirect the output of
this file to this other file.  In other words, file is redirected to stdin
of read W.  I hope this helps.  If I am wrong on these accounts, I will be
watching for the correct solution.

Scott Adkins
sadkins@oucsace.cs.ohiou.edu