Patch-ID: bash41-003
[bash.git] / examples / scripts.noah / y_or_n_p.bash
blob2674a2917f496a6a492d7a135fdfb749eda2da92
1 # y_or_n_p.bash
2 # Author: Noah Friedman <friedman@prep.ai.mit.edu>
3 # Created: 1992-06-18
4 # Last modified: 1993-03-01
5 # Public domain
7 # Conversion to bash v2 syntax done by Chet Ramey
9 # Commentary:
10 # Code:
12 #:docstring y_or_n_p:
13 # Usage: y_or_n_p QUERY
15 # Print QUERY on stderr, then read stdin for a y-or-n response. Actually,
16 # user may type anything they like, but first character must be a `y', `n',
17 # `q', or `!', otherwise the question is repeated until such an answer is
18 # obtained.
20 # If user typed `y', y_or_n_p returns 0.
22 # If user typed `n', y_or_n_p returns 1.
24 # If user typed `!', y_or_n_p returns 2. This is an indication to the
25 # caller that no more queries should be made. Assume `y' for all the rest.
27 # If user typed `q', y_or_n_p returns 3. This is an indication to the
28 # caller that no more queries should be made. Assume `n' for all the rest.
30 #:end docstring:
32 ###;;;autoload
33 function y_or_n_p ()
35 local ans
37 [ ! -t 0 ] && return 1
39 while read -p "$*" -e ans ; do
40 case "${ans}" in
41 y* | Y* ) return 0 ;;
42 n* | N* ) return 1 ;;
43 \! ) return 2 ;;
44 q* | Q* ) return 3 ;;
45 *) echo "Please answer one of \`y', \`n', \`q', or \`"\!"'" 1>&2 ;;
46 esac
47 done
50 #:docstring yes_or_no_p:
51 # Usage: yes_or_no_p QUERY
53 # Like y_or_n_p, but require a full `yes', `no', `yes!', or `quit' response.
54 #:end docstring:
56 ###;;;autoload
57 function yes_or_no_p ()
59 local ans
61 [ ! -t 0 ] && return 3
63 while read -p "$*" -e ans; do
64 ans="$(echo ${ans} | tr '[A-Z]' '[a-z]')"
66 case "${ans}" in
67 yes ) return 0 ;;
68 no ) return 1 ;;
69 yes\! ) return 2 ;;
70 quit ) return 3 ;;
71 *) echo "Please answer \`yes', \`no', \`yes"\!"', or \`quit'" 1>&2 ;;
72 esac
73 done
76 provide y_or_n_p
78 # y_or_n_p.bash ends here