Builtin 'read' data not saved

2014-01-02 Thread P Fudd
Hello all,

I'm at my wit's end.  This script is not working:
=
#!/bin/bash
B=none
echo "2" | while read -r A; do
  B="$A"
  echo 1.B=$B
done
echo 2.B=$B
==
The output, from GNU bash, version 4.2.25(1)-release
(x86_64-pc-linux-gnu), bash-4.2-2ubuntu2.1.deb, is this:

1.B=2
2.B=none

I'm baffled; it's like the while loop is in a parallel universe.  I tried
'export B=none' in line 2, with no effect.

Help?
--
f...@ch.pkts.ca



Re: Builtin 'read' data not saved

2014-01-02 Thread P Fudd
Here's some more oddities:

=failing.sh:
#!/bin/bash
R="1|2"
IFS='|' read -r A B <<< $R
echo A=$A, B=$B

Expected: "A=1, B=2"
Actual: "A=1 2, B="


fail2.sh:
#!/bin/bash
R="1|2"
while IFS='|' read -r A B; do
echo 1:A=$A, B=$B
done <<< $R
echo 2:A=$A, B=$B

Expected:
  1:A=1, B=2
  2:A=1, B=2
Actual:
  1:A=1, B=2
  2:A=, B=


GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)
bash-4.2-2ubuntu2.1.deb

Cheers!
--
f...@ch.pkts.ca
Thanks!



Re: Builtin 'read' data not saved

2014-01-02 Thread Davide Brini
On Thu, 2 Jan 2014 11:35:11 -0800, "P Fudd"  wrote:

> Here's some more oddities:
> 
> =failing.sh:
> #!/bin/bash
> R="1|2"
> IFS='|' read -r A B <<< $R
> echo A=$A, B=$B
> 
> Expected: "A=1, B=2"
> Actual: "A=1 2, B="
> 
> 
> fail2.sh:
> #!/bin/bash
> R="1|2"
> while IFS='|' read -r A B; do
> echo 1:A=$A, B=$B
> done <<< $R
> echo 2:A=$A, B=$B
> 
> Expected:
>   1:A=1, B=2
>   2:A=1, B=2
> Actual:
>   1:A=1, B=2
>   2:A=, B=
> 
> 
> GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)
> bash-4.2-2ubuntu2.1.deb

This is a FAQ, see http://mywiki.wooledge.org/BashFAQ/024 for a full
discussion.

-- 
D.



Re: Builtin 'read' data not saved

2014-01-02 Thread Davide Brini
On Thu, 2 Jan 2014 11:35:11 -0800, "P Fudd"  wrote:

> Here's some more oddities:

Ok, the link I sent you is more about the issue you describe in your first
message. See below for more on the new ones in this message.

> =failing.sh:
> #!/bin/bash
> R="1|2"
> IFS='|' read -r A B <<< $R
> echo A=$A, B=$B
> 
> Expected: "A=1, B=2"
> Actual: "A=1 2, B="

Here $R is expanded and wordsplitted using IFS before read is executed,
which means that $R does *not* contain any pipe symbol at the time "read"
is executed, so $A gets the whole thing.

> fail2.sh:
> #!/bin/bash
> R="1|2"
> while IFS='|' read -r A B; do
> echo 1:A=$A, B=$B
> done <<< $R
> echo 2:A=$A, B=$B
> 
> Expected:
>   1:A=1, B=2
>   2:A=1, B=2
> Actual:
>   1:A=1, B=2
>   2:A=, B=

This is slightly different, as when $R is expanded, IFS is still the
default, so it's expanded to "1|2" as expected, and the alternate IFS is
only in effect during the while/read loop. As expected, after the first
loop iteration $A is 1 and $B is 2. However read is executed once more, and
that second execution reads nothing, thus obviously setting both A and B
empty. Run with "set -x" and you'll see it yourself.

-- 
D.



Re: Builtin 'read' data not saved

2014-01-02 Thread Chet Ramey
On 1/2/14, 2:19 PM, P Fudd wrote:
> Hello all,
> 
> I'm at my wit's end.  This script is not working:

This comes up regularly.  The last time was less than a month ago:

http://lists.gnu.org/archive/html/bug-bash/2013-12/msg00066.html

If you have the Bash FAQ, this is covered in question E4.  Greg
Wooledge's FAQ also considers the topic:

http://mywiki.wooledge.org/BashFAQ/024

> I'm baffled; it's like the while loop is in a parallel universe.  

That is, in effect, what a pipeline is: separate processes run in
parallel with chained input and output.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



Re: Builtin 'read' data not saved

2014-01-02 Thread Chet Ramey
On 1/2/14, 2:35 PM, P Fudd wrote:
> Here's some more oddities:
> 
> =failing.sh:
> #!/bin/bash
> R="1|2"
> IFS='|' read -r A B <<< $R
> echo A=$A, B=$B
> 
> Expected: "A=1, B=2"
> Actual: "A=1 2, B="

This is a bug in bash-4.2.  The expansion of $R is not supposed to be
subject to the value of IFS.  You can read a discussion of the issue here:

http://lists.gnu.org/archive/html/bug-bash/2013-01/msg00041.html

> fail2.sh:
> #!/bin/bash
> R="1|2"
> while IFS='|' read -r A B; do
> echo 1:A=$A, B=$B
> done <<< $R
> echo 2:A=$A, B=$B

A and B will be unset when the second `read' hits EOF and returns failure,
breaking the loop.

Chet


-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/