Upload Tizen:Base source
[external/bash.git] / examples / scripts.noah / y_or_n_p.bash
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
6
7 # Conversion to bash v2 syntax done by Chet Ramey
8
9 # Commentary:
10 # Code:
11
12 #:docstring y_or_n_p:
13 # Usage: y_or_n_p QUERY
14 #
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.  
19 #
20 # If user typed `y', y_or_n_p returns 0.
21 #
22 # If user typed `n', y_or_n_p returns 1.
23 #
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. 
26 #
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.
29 #
30 #:end docstring:
31
32 ###;;;autoload
33 function y_or_n_p ()
34 {
35  local ans
36
37     [ ! -t 0 ] && return 1
38
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
48 }
49
50 #:docstring yes_or_no_p:
51 # Usage: yes_or_no_p QUERY
52 #
53 # Like y_or_n_p, but require a full `yes', `no', `yes!', or `quit' response. 
54 #:end docstring:
55
56 ###;;;autoload
57 function yes_or_no_p ()
58 {
59     local ans
60
61     [ ! -t 0 ] && return 3
62
63     while read -p "$*" -e ans; do
64         ans="$(echo ${ans} | tr '[A-Z]' '[a-z]')"
65
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
74 }
75
76 provide y_or_n_p
77
78 # y_or_n_p.bash ends here