Bug 177674

Summary: /bin/sh 'while read X' loop problem
Product: Base System Reporter: root <root>
Component: binAssignee: Jilles Tjoelker <jilles>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 9.1-RELEASE   
Hardware: Any   
OS: Any   

Description root 2013-04-06 21:40:00 UTC
There is some kind of problem with 'read' in a sh(1) loop,
but it is only demonstrated with some programs (such as mplayer).

Fix: 

I don't know what the problem is.  I tried inserting sleep(1)
commands to make the loop slow and other things, but nothing
made any difference.
How-To-Repeat: First, create a file with some valid MP3 pathanmes inside, then:

OK:   while read X; do echo    $X; done < FILEWITHMP3PATHNAMES
OK:   while read X; do ls -l   $X; done < FILEWITHMP3PATHNAMES
FAIL: while read X; do mplayer $X; done < FILEWITHMP3PATHNAMES

This last command line will fail in a mess of corruption,
and it is difficult to explain what is actually occurring.
With only 1 pathname in the file, it works OK, but with 2
or more, it increasingly results in more and more corruption.
Comment 1 poyopoyo 2013-04-06 21:53:49 UTC
At Sat, 6 Apr 2013 11:32:24 -0900 (AKST),
Joe Public wrote:
> OK:   while read X; do echo    $X; done < FILEWITHMP3PATHNAMES
> OK:   while read X; do ls -l   $X; done < FILEWITHMP3PATHNAMES
> FAIL: while read X; do mplayer $X; done < FILEWITHMP3PATHNAMES

The first iteration of mplayer eats up all stdin.
You can

while read X; do mplayer $X < /dev/tty; done

This is not a bug at all.

-- 
kuro
Comment 2 Jilles Tjoelker freebsd_committer freebsd_triage 2013-04-07 16:48:19 UTC
State Changed
From-To: open->closed

As described by kuro, this is not a bug. The mplayer reads from stdin, 
so that 'read' misses data. 

Try: 
while read X <&3; do mplayer $X; done 3<FILEWITHMP3PATHNAMES 


Comment 3 Jilles Tjoelker freebsd_committer freebsd_triage 2013-04-07 16:48:19 UTC
Responsible Changed
From-To: freebsd-bugs->jilles

Track replies.