|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Say Temp.txt contains the single line
RandomText The command read -r line < Temp.txt sets $line to "RandomText", but cat Temp.txt | read -r line does not. However, cat Temp.txt | while read -r line ; do echo "$line" ; done works. Why are they different? Isn't "read" reading form stdin in all cases? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Wed, 25 Jul 2007 18:56:14 -0700, Mister.Fred.Ma wrote:
> Say Temp.txt contains the single line > > RandomText > > The command > > read -r line < Temp.txt > > sets $line to "RandomText", but > > cat Temp.txt | read -r line > > does not. However, > > cat Temp.txt | while read -r line ; do echo "$line" ; done > > works. > > Why are they different? Isn't "read" reading form stdin in all cases? read is reading from stdin in all cases. What you are failing to see, which is explained in the FAQ, is the processes involved. If I add some grouping to show where different parts are run it would { cat Temp.txt } | { read -r line } { echo "$line" } Note the read and the echo are in different shells. { cat Temp.txt} | { while read -r line ; do echo "$line" ; done } Note the read and the echo are in the same shell. Note also that where to run these tasks is a shell implementation issue. ksh would run the first example as { cat Temp.txt } | { read -r line echo "$line" } so it would do what you clearly expect. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Jul 25, 11:31 pm, Icarus Sparry <use...@icarus.freeuk.com> wrote:
> On Wed, 25 Jul 2007 18:56:14 -0700, Mister.Fred.Ma wrote: > > Say Temp.txt contains the single line > > > RandomText > > > The command > > > read -r line < Temp.txt > > > sets $line to "RandomText", but > > > cat Temp.txt | read -r line > > > does not. However, > > > cat Temp.txt | while read -r line ; do echo "$line" ; done > > > works. > > > Why are they different? Isn't "read" reading form stdin in all cases? > > read is reading from stdin in all cases. What you are failing to see, > which is explained in the FAQ, is the processes involved. If I add some > grouping to show where different parts are run it would > > { cat Temp.txt } | { read -r line } > { echo "$line" } > > Note the read and the echo are in different shells. > > { cat Temp.txt} | { while read -r line ; do echo "$line" ; done } > > Note the read and the echo are in the same shell. > > Note also that where to run these tasks is a shell implementation issue. > ksh would run the first example as > > { cat Temp.txt } | { read -r line > echo "$line" } > > so it would do what you clearly expect. Hmmm. Thanks. And the man page also describes how pipelined commands execute in their own subshell. |
|
![]() |
| Outils de la discussion | |
|
|