It is currently Thu, 23 Oct 2014 02:19:44 GMT



 
Author Message
 How to flush stdin ?
Hello!

I am writing a shellscript that interacts with the user (an installation
script), and it presents various "screens" (or "panels") in which the
user can choose several options, or just take the default, by pressing
Enter. I would like to avoid that a default value is selected by
mistake, if the user presses Enter twice, or between "screens".
I figured that I could solve this by "flushing" stdin just before a new
screen is presented to the user. However, I have no clue how to do this.

Your help is very much aprecciated!

mario



 Fri, 29 Aug 2003 22:56:25 GMT   
 How to flush stdin ?
On Mon, 12 Mar 2001 14:56:25 GMT, Gianmario Scotti

while (getchar() != '/n') ;

but, don't use
  fflush(stdin);
because that won't work (at least not usually, and not always the way
you think it will work)

Lew Pitcher
Information Technology Consultant
Toronto Dominion Bank Financial Group

(Lew.Pitc...@td.com)

(Opinions expressed are my own, not my employer's.)



 Fri, 29 Aug 2003 23:16:22 GMT   
 How to flush stdin ?
On Mon, 12 Mar 2001 15:16:22 GMT, pit...@tdbank.ca (Lew Pitcher)
wrote:

<grin expression="sheepish">
Oops, my mistake. I didn't notice that this was a _shell_ program
being asked about. Please ignore my previous response.
</grin>

Lew Pitcher
Information Technology Consultant
Toronto Dominion Bank Financial Group

(Lew.Pitc...@td.com)

(Opinions expressed are my own, not my employer's.)



 Fri, 29 Aug 2003 23:26:24 GMT   
 How to flush stdin ?

cat <<\$L

Have a look at the 'read' command in the shell man-page. Some
shells support a time-out option; in which case you could
do a short time-out read just before the actual one (in a while
loop for the case of flushing multiple ENTERs) to achieve
a buffer flush. If you don't have this option in your version
of 'read' then you'd have to set it up to do so using 'stty'.

$L



 Sat, 30 Aug 2003 00:15:06 GMT   
 How to flush stdin ?
Lew Pitcher <pit...@tdbank.ca> wrote
        in <3aacea48.411978...@news21.on.aibn.com>:
...
#>while (getchar() != '/n') ;
#
# <grin expression="sheepish">
# Oops, my mistake. I didn't notice that this was a _shell_ program
# being asked about. Please ignore my previous response.
# </grin>

Even if this were comp.lang.c your solution is a no-no.
It loops indefinitely for EOF.

There are in fact (compiled) applications around that do what the
original poster wanted, i.e. ignore any typeahead up to a
program-defined point in the execution path. One way to implement this
in C I can imagine is to arrange for asynchronous I/O and install a
handler to just read whatever's there to be read. Once the program wants
to stop ignoring typeahead, it removes the async I/O handling.

You can trap SIGIO in a shell and do a "read foo". However I'm
at a loss on how to establish async I/O in the shell. Anyone?

Regards,

        Jens
--
Jens Schweikhardt  http://www.schweikhardt.net/
SIGSIG -- signature too long (core dumped)



 Sat, 30 Aug 2003 00:10:53 GMT   
 How to flush stdin ?

Embedding a Control-U (actually, the stty "kill" character) will
discard (not flush) the current input buffer. This is still useful,
as a read will never encounter non-flushed input previous to a newline
character (unless the user has entered a backslash-return in a read --
which is rare as this is an undocumented feature).

Another less portable method is (besides finding a flush-input
directive in your implementation of Unix's stty(1)) are:

stty -noflsh -flusho; trap : INTR; kill -INTR $$        # man: stty(1), noflsh

Thank you. This is a toughie; I only hope it can still be useful to you.

=Brian



 Sat, 30 Aug 2003 12:44:42 GMT   
 How to flush stdin ?
Hi Laura.

Thank you for your suggestion. I use korn shell. However, from the information I gathered
from the man pages of ksh, there is no time-out option for read. How would I use stty to
set "read" this way? This solution might be a winner.

mario



 Sat, 30 Aug 2003 17:46:07 GMT   
 How to flush stdin ?

cat <<\$L

All this stuff is unfortunately really system specific but there are 2
parameters 'MIN' and 'TIME' to the terminal IOCTL interface which allow
you to set a time-out on read (which is normal). Your 'stty' command
may or may not support these, if it does you should be able to work
something like this;

#!/usr/bin/ksh
echo type all those overflowing keypresses....
sleep 5
echo flushing buffer....
stty min 0 time 1 -icanon
while [ -n "`dd bs=1 count=1 2>/dev/nul`" ]
do
  echo flushed 1
done
stty min 1 time 0 icanon
echo please press RETURN
read a

Just keeps reading characters until one doesn't come in in 0.1 second,
then the 'dd' times out and doesn't get a character, so the terminal
is set back to normal and you can read your input.

Tested and workz fine on Linux, but outside of that I don't know....
As far as I can see though MIN, TIME & ICANON behaviour is all
standard, so maybe if you don't have control over them from 'stty'
you have to use a little program ( see termio(7) ).

$L



 Sun, 31 Aug 2003 10:11:05 GMT   
 How to flush stdin ?
brian hiles <b...@rainey.blueneptune.com> said:

How do you embed a Control-U in Bourne?  I tried various things like

^u
^U
echo "^u"
echo "^U"
echo "`^u`"

but to no avail.

This didn't work for me.  I tried it on two OS's,
a Linux system using gnu sh-utils stty and
on a full Unix distro, UnixWare 7.1.1.

Here's my simple example:
------------------------------------------------------------
#!/bin/sh
clear
echo "Please hit Enter a few times : \c"
read FOO
sleep 5
stty -noflsh -flusho; trap : 2; kill -2 $$
echo "Press Enter to exit : \c"
read BAR
echo "\n"
exit 0
-------------------------------------------------------------

The script always exits and doesn't flush stdin,
as far as I can tell.

Thanks,
Matthew



 Thu, 18 Sep 2003 15:48:32 GMT   
 How to flush stdin ?
In article <3ac6d400.368815...@news.pacbell.net>,
        mscha...@pacbell.net (Matt Schalit) writes:

Type "Control-V" "Control-U".
--

Frederick



 Fri, 19 Sep 2003 00:44:15 GMT   
 How to flush stdin ?
f...@enteract.com (Frederick Bruckman) said:

Hi Fredrick,
Thanks for the suggestion.  I tried this on both OS's,
and I can't get it to work.

On the full Unix distro, I used vi, and
pressing ctrl+v ctrl+u resulted in a line
that looked like this:
^U

I think that was the intended result.  When
I run this script, it errors with
  UX: sh: ./barf: ERROR: ^U: Not found

-------------------------------------
#!/bin/sh
clear
echo "Press enter a few times : \c"
read FOO
^U
echo "Now press Enter to exit : \c"
read BAR
exit 0
----------------------------------------

So the above didn't work.  I tried to make the
ctrl+v ctrl+u into an echo statement in case that's
what I'm supposed to do like this:
echo "^U"

That way it doesn't error, but it doesn't seem
to flush stdin either, because the script exits too soon.

On the Linux diskette OS, I don't have vi, but I have ae.
When I type ctrl+v ctrl+u on that OS, I see
^V^U

which I don't think is what you intended it to do.
The script with that in it also doesn't work using any
way I can think of, like
  echo "^V^U"

Sorry if I seem a bit lost with this, but I'm trying :)

Regards,
Matthew



 Fri, 19 Sep 2003 02:57:18 GMT   
 
   [ 11 post ] 

Similar Threads

1. enter newlines in terminal WITHOUT flushing stdin?

2. Korn Shell - Flushing Stdin

3. Redirection, Xterms, and Flushing STDIN

4. Result: How do I not flush stdin at end of program?

5. Flush STDIN in ksh/sh?

6. flushing stdin in /bin/sh?

7. Flushing stdin buffer before read

8. fetchmail flushing and not

9. keyboard buffer, flushing?


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software