flushing input prior to a read

flushing input prior to a read

Post by Jeffrey L. Cook » Fri, 12 Jun 1998 04:00:00



Anyone know a simple way of using shell commands (preferably Bourne or
Korn) to clean out any unwanted keyboard input prior to performing a
read?

My script does:

     prompt
     read
     <lot of processing>
     prompt
     read

Users tend to add garbage characters or <ENTER> prior to the read during
"<lot of processing>".

I've tried several variations such as:

     -  <&-    (close stdin -- although I'm not quite sure what this did
     nor how to open it again)
     -  </dev/null; read
     -  echo " " 0>/dev/null; read

Any suggestions?  I get the feeling I'm overlooking something obvious
... it can be this hard.

Thanks in advance.
--Jeff Cooke

 
 
 

flushing input prior to a read

Post by Donn Ca » Sat, 13 Jun 1998 04:00:00



| Anyone know a simple way of using shell commands (preferably Bourne or
| Korn) to clean out any unwanted keyboard input prior to performing a
| read?

|     prompt
|     read
|     <lot of processing>
|     prompt
|     read
|
| Users tend to add garbage characters or <ENTER> prior to the read during
| "<lot of processing>".

| I've tried several variations such as:
|
|     -  <&-    (close stdin -- although I'm not quite sure what this did
|     nor how to open it again)
|     -  </dev/null; read
|     -  echo " " 0>/dev/null; read

This isn't something you'd want to do routinely, since it violates the
normal expectations for input "typeahead" and makes it impossible to
run your script from anything but a tty.  And I suspect you're more
than smart enough to understand shell redirection better than the
above examples show, with a little reading and experimentation.

Here's a kind of crude approach.  Uses "cat" to dispose of the input,
while the processing runs, and then kills it to get input back.

  #!/bin/sh
  (read input from tty)...
  cat < /dev/tty > /dev/null &
  catnull=$!
  ...(process)...
  kill $catnull
  ...(read input from tty)

        Donn Cave, University Computing Services, University of Washington


 
 
 

1. Flushing input prior to a read

Anyone know a simple way of using shell commands (preferably Bourne or
Korn) to clean out any unwanted keyboard input prior to performing a
read?

My script does:

    prompt
    read
    <lot of processing>
    prompt
    read

Users tend to add garbage characters or <ENTER> prior to the read during
"<lot of processing>".

I've tried several variations such as:

    -  <&-    (close stdin -- although I'm not quite sure what this did
nor how to open it again)
    -  </dev/null; read
    -  echo " " 0>/dev/null; read

Any suggestions?  I get the feeling I'm overlooking something obvious
... it can be this hard.

Thanks in advance.
--Jeff Cooke

2. PPP/chat scripts

3. Flushing history to .history prior to logout?

4. Help with php4.1.2 and mysql

5. flush telnet input

6. Programming with Ctrl-Escape sequences

7. How do I flush keyboard inputs?

8. third level domains

9. flushing the input buffer in ksh

10. IOCTL flushing input buffer queues

11. HOWTO: flush inputs in Curses

12. flushing output/input

13. Can a PIPE input be FLUSHED?