Bash-4.3 distribution sources and documentation
[platform/upstream/bash.git] / examples / scripts / shprompt
1 #
2 # shprompt -- give a prompt and get an answer satisfying certain criteria
3 #
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
11 #
12 # Chet Ramey
13 # chet@ins.CWRU.Edu
14 #
15 #  Copyright 2002 Chester Ramey
16 #
17 #   This program is free software; you can redistribute it and/or modify
18 #   it under the terms of the GNU General Public License as published by
19 #   the Free Software Foundation; either version 2, or (at your option)
20 #   any later version.
21 #
22 #   TThis program is distributed in the hope that it will be useful,
23 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
24 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 #   GNU General Public License for more details.
26 #
27 #   You should have received a copy of the GNU General Public License
28 #   along with this program; if not, write to the Free Software Foundation,
29 #   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30
31 type=file
32
33 OPTS=dDfFsy
34
35 succeed()
36 {
37         echo "$1"
38         exit 0
39 }
40
41 while getopts "$OPTS" c
42 do
43         case "$c" in
44         s)      type=string
45                 ;;
46         f)      type=file
47                 ;;
48         F)      type=path
49                 ;;
50         d)      type=dir
51                 ;;
52         D)      type=dirpath
53                 ;;
54         y)      type=yesno
55                 ;;
56         ?)      echo "usage: $0 [-$OPTS] prompt" 1>&2
57                 exit 2
58                 ;;
59         esac
60 done
61
62 if [ "$OPTIND" -gt 1 ] ; then
63         shift $(( $OPTIND - 1 ))
64 fi
65
66 while :
67 do
68         case "$type" in
69         string)
70                 echo -n "$1" 1>&2
71                 read ans || exit 1
72                 if [ -n "$ans" ] ; then
73                         succeed "$ans"
74                 fi
75                 ;;
76         file|path)
77                 echo -n "$1" 1>&2
78                 read ans || exit 1
79                 #
80                 # use `fn' and eval so that bash will do tilde expansion for
81                 # me
82                 #
83                 eval fn="$ans"
84                 case "$fn" in
85                 /*)     if test -e "$fn" ; then
86                                 succeed "$fn"
87                         else
88                                 echo "$0: '$fn' does not exist" 1>&2
89                         fi
90                         ;;
91                 *)      if [ "$type" = "path" ] ; then
92                                 echo "$0: must give full pathname to file" 1>&2
93                         else
94                                 if test -e "$fn" ; then
95                                         succeed "$fn"
96                                 else
97                                         echo "$0: '$fn' does not exist" 1>&2
98                                 fi
99                         fi
100                         ;;
101                 esac
102                 ;;
103         dir|dirpath)
104                 echo -n "$1" 1>&2
105                 read ans || exit 1
106                 #
107                 # use `fn' and eval so that bash will do tilde expansion for
108                 # me
109                 #
110                 eval fn="$ans"
111                 case "$fn" in
112                 /*)     if test -d "$fn" ; then
113                                 succeed "$fn"
114                         elif test -e "$fn" ; then
115                                 echo "$0 '$fn' is not a directory" 1>&2
116                         else
117                                 echo "$0: '$fn' does not exist" 1>&2
118                         fi
119                         ;;
120                 *)      if [ "$type" = "dirpath" ] ; then
121                                 echo "$0: must give full pathname to directory" 1>&2
122                         else
123                                 if test -d "$fn" ; then
124                                         succeed "$fn"
125                                 elif test -e "$fn" ; then
126                                         echo "$0 '$fn' is not a directory" 1>&2
127                                 else
128                                         echo "$0: '$fn' does not exist" 1>&2
129                                 fi
130                         fi
131                         ;;
132                 esac
133                 ;;
134         yesno)
135                 echo -n "$1" 1>&2
136                 read ans || exit 1
137                 case "$ans" in
138                 y|Y|[yY][eE][sS])
139                         succeed "yes"
140                         ;;
141                 n|N|[nN][oO])
142                         succeed "no"
143                         exit 0
144                         ;;
145                 *)
146                         echo "$0: yes or no required" 1>&2
147                         ;;
148                 esac
149                 ;;
150         esac
151 done
152
153 exit 1