source: vendor/bash/3.1-p17/examples/scripts/shprompt

Last change on this file was 3228, checked in by bird, 18 years ago

bash 3.1

File size: 2.2 KB
Line 
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
15type=file
16
17OPTS=dDfFsy
18
19succeed()
20{
21 echo "$1"
22 exit 0
23}
24
25while getopts "$OPTS" c
26do
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
44done
45
46if [ "$OPTIND" -gt 1 ] ; then
47 shift $[$OPTIND - 1]
48fi
49
50while :
51do
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
135done
136
137exit 1
Note: See TracBrowser for help on using the repository browser.