Keyboard input/Obtain a Y or N response: Difference between revisions

→‎{{header|UNIX Shell}}: Add shell version
(→‎{{header|UNIX Shell}}: Add shell version)
Line 678:
 
</lang>
 
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
<lang bash>getkey() {
local stty="$(stty -g)"
trap "stty $stty; trap SIGINT; return 128" SIGINT
stty cbreak -echo
local key
while true; do
key=$(dd count=1 2>/dev/null) || return $?
if [ -z "$1" ] || [[ "$key" == [$1] ]]; then
break
fi
done
stty $stty
echo "$key"
return 0
}
 
yorn() {
echo -n "${1:-Press Y or N to continue: }" >&2
local yorn="$(getkey YyNn)" || return $?
case "$yorn" in
[Yy]) echo >&2 Y; return 0;;
[Nn]) echo >&2 N; return 1;;
esac
}</lang>
 
=={{header|Vedit macro language}}==
1,480

edits