Patch-ID: bash40-021
[bash.git] / examples / scripts / shprompt
blobec8b997ad9b02442236c7f9b2abde70b32e302f5
2 # shprompt -- give a prompt and get an answer satisfying certain criteria
4 # shprompt [-dDfFsy] prompt
5 #       s = prompt for string
6 #       f = prompt for filename
7 #       F = prompt for full pathname to a file or directory
8 #       d = prompt for a directory name
9 #       D = prompt for a full pathname to a directory
10 #       y = prompt for y or n answer
12 # Chet Ramey
13 # chet@ins.CWRU.Edu
15 type=file
17 OPTS=dDfFsy
19 succeed()
21         echo "$1"
22         exit 0
25 while getopts "$OPTS" c
27         case "$c" in
28         s)      type=string
29                 ;;
30         f)      type=file
31                 ;;
32         F)      type=path
33                 ;;
34         d)      type=dir
35                 ;;
36         D)      type=dirpath
37                 ;;
38         y)      type=yesno
39                 ;;
40         ?)      echo "usage: $0 [-$OPTS] prompt" 1>&2
41                 exit 2
42                 ;;
43         esac
44 done
46 if [ "$OPTIND" -gt 1 ] ; then
47         shift $[$OPTIND - 1]
50 while :
52         case "$type" in
53         string)
54                 echo -n "$1" 1>&2
55                 read ans || exit 1
56                 if [ -n "$ans" ] ; then
57                         succeed "$ans"
58                 fi
59                 ;;
60         file|path)
61                 echo -n "$1" 1>&2
62                 read ans || exit 1
63                 #
64                 # use `fn' and eval so that bash will do tilde expansion for
65                 # me
66                 #
67                 eval fn="$ans"
68                 case "$fn" in
69                 /*)     if test -e "$fn" ; then
70                                 succeed "$fn"
71                         else
72                                 echo "$0: '$fn' does not exist" 1>&2
73                         fi
74                         ;;
75                 *)      if [ "$type" = "path" ] ; then
76                                 echo "$0: must give full pathname to file" 1>&2
77                         else
78                                 if test -e "$fn" ; then
79                                         succeed "$fn"
80                                 else
81                                         echo "$0: '$fn' does not exist" 1>&2
82                                 fi
83                         fi
84                         ;;
85                 esac
86                 ;;
87         dir|dirpath)
88                 echo -n "$1" 1>&2
89                 read ans || exit 1
90                 #
91                 # use `fn' and eval so that bash will do tilde expansion for
92                 # me
93                 #
94                 eval fn="$ans"
95                 case "$fn" in
96                 /*)     if test -d "$fn" ; then
97                                 succeed "$fn"
98                         elif test -e "$fn" ; then
99                                 echo "$0 '$fn' is not a directory" 1>&2
100                         else
101                                 echo "$0: '$fn' does not exist" 1>&2
102                         fi
103                         ;;
104                 *)      if [ "$type" = "dirpath" ] ; then
105                                 echo "$0: must give full pathname to directory" 1>&2
106                         else
107                                 if test -d "$fn" ; then
108                                         succeed "$fn"
109                                 elif test -e "$fn" ; then
110                                         echo "$0 '$fn' is not a directory" 1>&2
111                                 else
112                                         echo "$0: '$fn' does not exist" 1>&2
113                                 fi
114                         fi
115                         ;;
116                 esac
117                 ;;
118         yesno)
119                 echo -n "$1" 1>&2
120                 read ans || exit 1
121                 case "$ans" in
122                 y|Y|[yY][eE][sS])
123                         succeed "yes"
124                         ;;
125                 n|N|[nN][oO])
126                         succeed "no"
127                         exit 0
128                         ;;
129                 *)
130                         echo "$0: yes or no required" 1>&2
131                         ;;
132                 esac
133                 ;;
134         esac
135 done
137 exit 1