I'm writing a Korn shell script, and it will read input from the user. I would like to clear the keyboard buffer before calling the "read" command, so that the user doesn't cause problems by impatiently typing ahead. I might add that the intended audience for this script is myself. 🙂
I'm using the OpenBSD ksh, which appears to be a variant of ksh88. I do have ksh93 available, but would prefer to NOT rely on Bash.
Example code:
# Would like to clear stdin buffer here
echo "Are you sure?"
read input
case $input in
y*|Y*)
echo "You said yes!"
;;
*)
echo "You said no!"
exit 1
;;
esac
EDIT: Basically I want to clear ALL data from stdin before calling the "read".
I'm currently experimenting with setting a signal handler for SIGALRM and using trap to implement a timeout on doing a "pre-read".