PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > bash read & piping
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

bash read & piping

Réponse
 
LinkBack Outils de la discussion
Vieux 26/07/2007, 02h56   #1
Mister.Fred.Ma@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut bash read & piping

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?

  Réponse avec citation
Vieux 26/07/2007, 04h31   #2
Icarus Sparry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bash read & piping

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.
  Réponse avec citation
Vieux 28/07/2007, 01h25   #3
Mister.Fred.Ma@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bash read & piping

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.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 14h05.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12738 seconds with 11 queries