Linux Archive

Linux Archive (http://www.linux-archive.org/)
-   Debian User (http://www.linux-archive.org/debian-user/)
-   -   read command not reading from pipe. why? (http://www.linux-archive.org/debian-user/304245-read-command-not-reading-pipe-why.html)

Lorenzo Beretta 01-04-2010 06:52 PM

read command not reading from pipe. why?
 
Foss User ha scritto:

I see that the read command stores input entered only on the console
into the variables. Example:

$ read a
foo
$ echo $a
foo

But when I don't enter input on the console by keyboard, but pipe it
into the standard input of read, I am unable to store the input into
the variable.

$ echo bar | read a
$ echo $a
foo

However, the help of read says this:

$ help read
read: read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-p
prompt] [-t timeout] [-u fd] [name ...]
Read a line from the standard input and split it into fields.

So, why is it not reading the line from standard input when input is
passed to it using a pipe?


Because the "read" is done in a child process, that starts, executes its
instructions (ie: read), modifies its own private environment, and then
dies, leaving the parent's environment untouched.

Try:

$echo qwerty | while read a ; do echo "a=$a" ; done
a=qwerty
$echo "a=$a"
a=


Hope that helped


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

Mark Allums 01-04-2010 08:12 PM

read command not reading from pipe. why?
 
On 1/4/2010 1:10 PM, Foss User wrote:

I see that the read command stores input entered only on the console
into the variables. Example:

$ read a
foo
$ echo $a
foo

But when I don't enter input on the console by keyboard, but pipe it
into the standard input of read, I am unable to store the input into
the variable.

$ echo bar | read a
$ echo $a
foo

However, the help of read says this:

$ help read
read: read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-p
prompt] [-t timeout] [-u fd] [name ...]
Read a line from the standard input and split it into fields.

So, why is it not reading the line from standard input when input is
passed to it using a pipe?





Try this:

$ echo bar > /tmp/foo; read a < /tmp/foo; echo $a; rm /tmp/foo

That's may not be the best approach (obviously), but if you understand
what's going on above, then you can use read. The problem is that each
command gets its own copy of the environment, so your


$ echo $a

is echoing its *own* a, which is empty. Don't try this, either:

$ echo bar | read a; export a
$ echo $a

Won't work. The piped

$ echo bar

and the

read a

are still each in their own world.


Mark Allums


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

Eric Meijer 01-09-2010 09:28 AM

read command not reading from pipe. why?
 
Mark Allums wrote:

On 1/4/2010 1:10 PM, Foss User wrote:

I see that the read command stores input entered only on the console
into the variables. Example:

$ read a
foo
$ echo $a
foo

But when I don't enter input on the console by keyboard, but pipe it
into the standard input of read, I am unable to store the input into
the variable.

$ echo bar | read a
$ echo $a
foo
As others pointed out, the variable a that is read is in another shell.
If you want to do something like this, you need to turn it around:


a=$(echo bar)

Eric


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org


All times are GMT. The time now is 12:06 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.