Created
March 2, 2012 11:07
-
-
Save mjf/1957793 to your computer and use it in GitHub Desktop.
ASK - asks Yes/no question (with signal handling)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh | |
# ASK - asks Yes/no question (with signal handling) | |
# Copyright (C) 2011 Matous J. Fialka, <http://mjf.cz/> | |
# Released under the Terms of The MIT License | |
signal_handler() | |
{ | |
printf -- 'Interrupted.' | |
trap - 2 | |
kill -2 $$ | |
exit 130 | |
} | |
trap signal_handler 1 2 3 13 15 | |
ask=Yes/no | |
if [ $# -gt 0 ] | |
then | |
case $1 in | |
-h|-help) | |
printf -- 'ask [-hn] text\n' | |
exit 0 | |
;; | |
-n|-no) | |
ask=No/yes | |
shift | |
;; | |
--) | |
shift | |
;; | |
esac | |
fi | |
while : | |
do | |
printf -- '%s' "$*" | |
if [ $# -gt 0 ] | |
then | |
printf -- ' (' | |
fi | |
case $ask in | |
Yes/no|'') | |
printf -- Yes/no | |
;; | |
No/yes) | |
printf -- No/yes | |
;; | |
esac | |
if [ $# -gt 0 ] | |
then | |
printf -- ')' | |
else | |
printf -- '?' | |
fi | |
printf -- ' ' | |
read -s -n 1 answer | |
case "$answer" in | |
'') | |
case $ask in | |
Yes/no|'') | |
printf -- 'Certainly.\n' | |
exit 0 | |
;; | |
No/yes) | |
printf -- 'Certainly not.\n' | |
exit 1 | |
;; | |
esac | |
;; | |
y|Y) | |
printf -- 'Yes.\n' | |
exit 0 | |
;; | |
n|N) | |
printf -- 'No.\n' | |
exit 1 | |
;; | |
*) | |
printf -- 'Wrong answer.\n' | |
;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment