Date: Sat, 8 Jul 2006 01:23:43 +0300 From: Giorgos Keramidas <keramida@ceid.upatras.gr> To: Jan Grant <jan.grant@bristol.ac.uk> Cc: freebsd-standards@freebsd.org Subject: Re: stdio/sh behaviour guaranteed? Message-ID: <20060707222343.GA10756@gothmog.pc> In-Reply-To: <20060707201402.T14116@tribble.ilrt.bris.ac.uk> References: <20060707201402.T14116@tribble.ilrt.bris.ac.uk>
next in thread | previous in thread | raw e-mail | index | archive | help
On 2006-07-07 20:23, Jan Grant <jan.grant@bristol.ac.uk> wrote: > Consider the following snippet. > > [[[ > #!/bin/sh > > echo one > t > while read line > do > echo $line > case $line in > one) echo two >> t > ;; > two) echo three >> t > ;; > esac > done <t > ]]] > > This produces three lines of output on FreeBSD: which is what > I'd intuitively expect and it's certainly useful behaviour. > > I'm just trying to determine if that behaviour is one that I > can rely on - in other words, I guess, if stdio performs a > "short read" that fails to fill a buffer, and the underlying > file is then extended outside the process, will another attempt > to read from the FILE* (or a test of feof, say) honour the new, > longer file contents? I think that /bin/sh is not absolutely required to use stdio.h for input (which could pre-read some text and cause the above to fail). Having said that, I think it makes sense to assume that input is line-buffered, otherwise stuff like this could fail: cmd | while read line ; do echo "$line" ; done > And in particular, is the idiom above blessed by appropriate > posix standards? Not sure, but the behavior seems to be the same in Solaris too, with a variety of shells: | kobe % cat /etc/release | Solaris 10 1/06 s10x_u1wos_19a X86 | Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. | Use is subject to license terms. | Assembled 07 December 2005 | kobe % pwd | /tmp/t | kobe % cat -n foo.sh | 1 #!/bin/sh | 2 | 3 echo one > t | 4 while read line | 5 do | 6 echo $line | 7 case $line in | 8 one) echo two >> t | 9 ;; | 10 two) echo three >> t | 11 ;; | 12 esac | 13 done <t | kobe % /bin/sh foo.sh | one | two | three | kobe % /sbin/sh foo.sh | one | two | three | kobe % /usr/xpg4/bin/sh foo.sh | one | two | three | kobe % ksh foo.sh | one | two | three | kobe % bash foo.sh | one | two | three | kobe % zsh foo.sh | one | two | three | kobe %
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20060707222343.GA10756>