Subject: Re: bug in sh?
To: None <current-users@NetBSD.ORG>
From: der Mouse <mouse@Collatz.McRCIM.McGill.EDU>
List: current-users
Date: 05/25/1995 11:17:24
> Could someone tell me if this is proper behavior for sh, a known bug,
> or a newly discovered critter?

> #!/bin/sh -
> grep "^$1" ./data | read key data1 data2 data3
> echo $key - $data1 - $data2 - $data3

[the echo indicates that $key, $data1, $data2, $data3 are all empty]

> #!/bin/sh -
> grep "^$1" ./data | while read key data1 data2 data3
> do
>    echo $key - $data1 - $data2 - $data3
> done

[this one works]

> Notice that the read command does not appear to want to do anything
> unless it has a loop to work in.

I think the difference is not so much the presence of the loop as that
the echo and read are being executed by the same shell.  In the first
case, piping something into the read forces it to be executed in a
subshell, so the echo doesn't get the variable settings.  In the second
case, the entire while loop is pushed into the subshell, so the
variable settings survive between the read and the echo.  All Bourne
shells I've seen work this way.


grep "^$1" ./data |
( read key data1 data2 data3 ; echo $key - $data1 - $data2 - $data3 )

so that the read and echo are executed in the same shell.

					der Mouse