Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #11471

Re: Why does a Bash shell script write prompts followed by reads and do it right?

Path csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!usenet.stanford.edu!not-for-mail
From Eric Blake <eblake@redhat.com>
Newsgroups gnu.bash.bug
Subject Re: Why does a Bash shell script write prompts followed by reads and do it right?
Date Wed, 2 Sep 2015 14:55:25 -0600
Organization Red Hat, Inc.
Lines 66
Approved bug-bash@gnu.org
Message-ID <mailman.424.1441227338.19560.bug-bash@gnu.org> (permalink)
References <CAAykW6m4H2sSshWNfv3mQr9q6ScKzLebqMBgEo96vRUSBY6SAQ@mail.gmail.com> <1441218426.1517.2.camel@16bits.net> <CAAykW6mnTcLA21h79YDb_4-Pv3GhG_8xBhhNcG0unzYpkfqGMQ@mail.gmail.com> <20150902141935720946809@bob.proulx.com>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="uF7Dblg6f6GJBo9DPF7LD5QvH7BveVX6E"
X-Trace usenet.stanford.edu 1441227338 11326 208.118.235.17 (2 Sep 2015 20:55:38 GMT)
X-Complaints-To action@cs.stanford.edu
To Robert Parker <rlp1938@gmail.com>, bug-bash@gnu.org
Envelope-to bug-bash@gnu.org
Openpgp url=http://people.redhat.com/eblake/eblake.gpg
X-Enigmail-Draft-Status N1110
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0
In-Reply-To <20150902141935720946809@bob.proulx.com>
X-Scanned-By MIMEDefang 2.68 on 10.5.11.26
X-detected-operating-system by eggs.gnu.org: GNU/Linux 3.x
X-Received-From 209.132.183.28
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.14
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <http://lists.gnu.org/archive/html/bug-bash>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
Xref csiph.com gnu.bash.bug:11471

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On 09/02/2015 02:22 PM, Bob Proulx wrote:
> Robert Parker wrote:
>>         fputs(shortprompt, stdout);
>>         fflush(stdin);
> 
> Uhm...  fflush'ing stdin?  That doesn't make sense.

There is one case where fflush(stdin) is useful on a terminal: it lets
you discard any pending input.  For example, if you have just prompted
for a password and altered the tty to not echo what is typed, then
flushing stdin prior to reading the password is a nice step to ensure
that any accidental type-ahead data entered before the prompt doesn't
get mixed up with the password attempt, particularly since the lack of
echoing won't show the user that the password got munged.

Additionally, fflush(stdin) on a regular file is useful for applications
that might be used in a sequence of processes, and which have scenarios
where they only partially consuming input; POSIX requires that such
applications should reset the file pointer to the last byte actually
processed so that the next process starts processing at the next byte
rather than skipping ahead by however many bytes were cached early but
left unprocessed.  Example: { sed -n 1q; cat; } < file | ... - for the
'cat' to pass the remaining lines to the rest of the pipeline, the 'sed'
must rewind back to the end of the first line even if it read more than
that.

But in most coding scenarios, flushing stdin is rather rare; it wasn't
even well-specified by POSIX until 2012.

>  Obviously you
> meant to flush stdout.

And in the code snippet shown here, this observation is certainly correct.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Back to gnu.bash.bug | Previous | Next | Find similar


Thread

Re: Why does a Bash shell script write prompts followed by reads and do it right? Eric Blake <eblake@redhat.com> - 2015-09-02 14:55 -0600

csiph-web