From: Jilles Tjoelker <jilles@stack.nl>
To: Steve Schnepp <steve.schnepp@gmail.com>
Cc: dash@vger.kernel.org, 595063@bugs.debian.org
Subject: Re: read() builtin doesn't read integer value /proc files (but bash's does)
Date: Sat, 4 Sep 2010 21:35:04 +0200 [thread overview]
Message-ID: <20100904193504.GA3357@stack.nl> (raw)
In-Reply-To: <AANLkTi=d_13RCDhJHE_23BXN5Tf2RY_2e3rED7JwtFOK@mail.gmail.com>
On Sat, Sep 04, 2010 at 08:20:33PM +0200, Steve Schnepp wrote:
> 2010/9/3 Jilles Tjoelker <jilles@stack.nl>:
> > This patch assumes that the file descriptor is discarded afterwards (its
> > position does not matter). Therefore the very common construct
> > while read x; do
> > ...
> > done
> > stops working.
> Ohh.. thanks for that, I didn't see it.
> Actually "while read x" continues to work.
> But "reopening the file" doesn't as in :
> read a b < datafile
> echo ${a} ${b}
> read a b < datafile
> echo ${a} ${b}
You're right, it's even stranger than I expected.
> I attached an updated patch that corrects this pb by discarding the
> buffer when opening a new file.
This discarding is still bad as it throws away valid data if the open
file description is shared. This happens if stdin is redirected inside a
while read... loop.
Furthermore, I think constructions like
read x; cat
and
read x; (read y); read z
should keep working. This requires that the input's file position be
synced whenever another process may see it (fork/exit). Due to the
highly dynamic character of the shell and the common use of fd 0, this
probably means that you can't do better than syncing after each read
builtin. (For example, 'read' could be overridden with a function after
the third line.)
Another thought:
exec 3<&0; read x; read y <&3
or even
sh -c 'read x; read y <&3' 3<&0
Different file descriptors may refer to the same open file description
and the shell may not know this.
--
Jilles Tjoelker
next prev parent reply other threads:[~2010-09-04 19:35 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-09-01 8:10 read() builtin doesn't read integer value /proc files (but bash's does) Steve Schnepp
2010-09-02 15:02 ` Steve Schnepp
2010-09-03 21:25 ` Jilles Tjoelker
2010-09-04 18:20 ` Steve Schnepp
2010-09-04 19:35 ` Jilles Tjoelker [this message]
2010-11-28 8:42 ` Herbert Xu
2010-12-15 9:49 ` read() builtin doesnt read integer value /proc files (but bashs does) Cristian Ionescu-Idbohrn
2010-12-15 18:55 ` Jonathan Nieder
2010-12-15 19:12 ` Cristian Ionescu-Idbohrn
2010-12-18 22:23 ` Jilles Tjoelker
2010-09-02 19:09 ` read() builtin doesn't read integer value /proc files (but bash's does) Jilles Tjoelker
2010-09-03 9:23 ` Steve Schnepp
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20100904193504.GA3357@stack.nl \
--to=jilles@stack.nl \
--cc=595063@bugs.debian.org \
--cc=dash@vger.kernel.org \
--cc=steve.schnepp@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox