Why `echo -n hello | while read v; do echo $v; done' prints nothing?

2010-12-02 Thread Clark J. Wang
Following command also prints nothing, confused :(

for ((i = 0; i < 10; ++i)); do echo -n " $i"; done | while read v; do echo
$v; done

-- 
Clark


Re: Why `echo -n hello | while read v; do echo $v; done' prints nothing?

2010-12-02 Thread Eric Blake
On 12/02/2010 04:04 AM, Clark J. Wang wrote:
> Following command also prints nothing, confused :(
> 
> for ((i = 0; i < 10; ++i)); do echo -n " $i"; done | while read v; do echo
> $v; done

http://www.faqs.org/faqs/unix-faq/shell/bash/
FAQ E4.

-- 
Eric Blake   ebl...@redhat.com+1-801-349-2682
Libvirt virtualization library http://libvirt.org



signature.asc
Description: OpenPGP digital signature


Re: Why `echo -n hello | while read v; do echo $v; done' prints nothing?

2010-12-02 Thread Roman Rakus

On 12/02/2010 12:04 PM, Clark J. Wang wrote:

Following command also prints nothing, confused :(

for ((i = 0; i<  10; ++i)); do echo -n " $i"; done | while read v; do echo
$v; done


read wants to read one line, but you don't end your line.
try this two examples:
$ printf '1 2 3 4' | while read v; do echo $v; done
$ printf '1 2 3 4\n' | while read v; do echo $v; done

RR



Re: Why `echo -n hello | while read v; do echo $v; done' prints nothing?

2010-12-02 Thread Greg Wooledge
On Thu, Dec 02, 2010 at 07:04:57PM +0800, Clark J. Wang wrote:
> Following command also prints nothing, confused :(
> 
> for ((i = 0; i < 10; ++i)); do echo -n " $i"; done | while read v; do echo
> $v; done

The output from the first command in the pipeline does not end with a
newline.  Therefore, 'read' in the second command returns 'failure'
(non-zero) when it reads the first line of input, and your loop never
iterates.



Re: Why `echo -n hello | while read v; do echo $v; done' prints nothing?

2010-12-02 Thread Andreas Schwab
"Clark J. Wang"  writes:

> Following command also prints nothing, confused :(
>
> for ((i = 0; i < 10; ++i)); do echo -n " $i"; done | while read v; do echo
> $v; done

$ printf . | { read v; echo $?; }
1

Andreas.

-- 
Andreas Schwab, sch...@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."



Re: Why `echo -n hello | while read v; do echo $v; done' prints nothing?

2010-12-02 Thread Greg Wooledge
On Thu, Dec 02, 2010 at 09:29:29AM -0700, Eric Blake wrote:
> On 12/02/2010 04:04 AM, Clark J. Wang wrote:
> > Following command also prints nothing, confused :(
> > 
> > for ((i = 0; i < 10; ++i)); do echo -n " $i"; done | while read v; do echo
> > $v; done
> 
> http://www.faqs.org/faqs/unix-faq/shell/bash/
> FAQ E4.

That was my first thought as well, upon first glance.  But that's not
the actual problem here.  It's the lack of newline causing read to return
'failure' even though it does read the input.




Re: Why `echo -n hello | while read v; do echo $v; done' prints nothing?

2010-12-02 Thread William Park
On Thu, Dec 02, 2010 at 11:33:24AM -0500, Greg Wooledge wrote:
> On Thu, Dec 02, 2010 at 09:29:29AM -0700, Eric Blake wrote:
> > On 12/02/2010 04:04 AM, Clark J. Wang wrote:
> > > Following command also prints nothing, confused :(
> > > 
> > > for ((i = 0; i < 10; ++i)); do echo -n " $i"; done | while read v; do echo
> > > $v; done
> > 
> > http://www.faqs.org/faqs/unix-faq/shell/bash/
> > FAQ E4.
> 
> That was my first thought as well, upon first glance.  But that's not
> the actual problem here.  It's the lack of newline causing read to return
> 'failure' even though it does read the input.

Yeah, it has bitten me too many times.  I keep forgetting it though.

-- 
William



Re: echo "${HOME#$*/}" segfaults

2010-12-02 Thread Chet Ramey
On 11/30/10 8:43 PM, David Rochberg wrote:

> Bash Version: 4.1
> Patch Level: 5
> Release Status: release
> 
> Description:
> 
> echo "${HOME#$*/}" segfaults

Thanks for the report.

-- 
``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: Why `echo -n hello | while read v; do echo $v; done' prints nothing?

2010-12-02 Thread Clark J. Wang
On Thu, Dec 2, 2010 at 11:49 PM, Greg Wooledge  wrote:

> On Thu, Dec 02, 2010 at 07:04:57PM +0800, Clark J. Wang wrote:
> > Following command also prints nothing, confused :(
> >
> > for ((i = 0; i < 10; ++i)); do echo -n " $i"; done | while read v; do
> echo
> > $v; done
>
> The output from the first command in the pipeline does not end with a
> newline.  Therefore, 'read' in the second command returns 'failure'
> (non-zero) when it reads the first line of input, and your loop never
> iterates.
>

But is that reasonable? I think read should return success in this case
which makes more sense to me. Does the POSIX standards require that?

-- 
Clark


Re: Why `echo -n hello | while read v; do echo $v; done' prints nothing?

2010-12-02 Thread Eric Blake
On 12/02/2010 07:02 PM, Clark J. Wang wrote:
>> The output from the first command in the pipeline does not end with a
>> newline.  Therefore, 'read' in the second command returns 'failure'
>> (non-zero) when it reads the first line of input, and your loop never
>> iterates.
> 
> But is that reasonable? I think read should return success in this case
> which makes more sense to me. Does the POSIX standards require that?

POSIX requires that the input to read be a text file (and by the
definition of text file in POSIX, it must either be empty or end in a
newline).  By violating POSIX and passing something that does not end in
a newline, you are no longer bound by the rules of POSIX.  Therefore, it
would be a reasonable bash extension that read could return 0 status if
it read data that did not end in a newline, but it would not be a
standard-compliant script that relied on such an extension.  You're
better off supplying the trailing newline, and guaranteeing a compliant
usage.

-- 
Eric Blake   ebl...@redhat.com+1-801-349-2682
Libvirt virtualization library http://libvirt.org



signature.asc
Description: OpenPGP digital signature