| Previous | Table of Contents | Next |
Listing 21.1 contains a complete listing of the shell library, libTYSP.sh, discussed in this chapter. The line numbers are present for your reference and should not be entered into a script library file that you plan to use.
Listing 21.1Complete Listing of the Shell Library libTYSP.sh
1 #!/bin/sh
2
3 ################################################
4 # Name: printERROR
5 # Desc: prints an message to STDERR
6 # Args: $@ -> message to print
7 ################################################
8
9 printERROR() {
10 echo "ERROR:" $@ >&2
11 }
12
13 ################################################
14 # Name: printWARNING
15 # Desc: prints an message to STDERR
16 # Args: $@ -> message to print
17 ################################################
18
19 printWARNING() {
20 echo "WARNING:" $@ >&2
21 }
22
23 ################################################
24 # Name: printUSAGE
25 # Desc: prints a USAGE message and then exits
26 # Args: $@ -> message to print
27 ################################################
28
29 printUSAGE() {
30 echo "USAGE:" $@
31 exit
32 }
33
34 ################################################
35 # Name: promptYESNO
36 # Desc: ask a yes/no question
37 # Args: $1 -> The prompt
38 # $2 -> The default answer (optional)
39 # Vars: YESNO -> set to the users response
40 # y for yes, n for no
41 ################################################
42
43 promptYESNO() {
44
45 if [ $# -lt 1 ] ; then
46 printERROR "Insufficient Arguments."
47 return 1
48 fi
49
50 DEF_ARG=""
51 YESNO=""
52
53 case "$2" in
54 [yY]|[yY][eE][sS])
55 DEF_ARG=y ;;
56 [nN]|[nN][oO])
57 DEF_ARG=n ;;
58 esac
59
60 while :
61 do
62
63 printf "$1 (y/n)? "
64
65 if [ -n "$DEF_ARG" ] ; then
66 printf "[$DEF_ARG] "
67 fi
68
69 read YESNO
70
71 if [ -z "$YESNO" ] ; then
72 YESNO="$DEF_ARG"
73 fi
74
75 case "$YESNO" in
76 [yY]|[yY][eE][sS])
77 YESNO=y ; break ;;
78 [nN]|[nN][oO])
79 YESNO=n ; break ;;
80 *)
81 YESNO="" ;;
82 esac
83
84 done
85
86 export YESNO
87 unset DEF_ARG
88 return 0
89 }
90
91 ################################################
92 # Name: promptRESPONSE
93 # Desc: ask a question
94 # Args: $1 -> The prompt
95 # $2 -> The default answer (optional)
96 # Vars: RESPONSE -> set to the users response
97 ################################################
98
99 promptRESPONSE() {
100
101 if [ $# -lt 1 ] ; then
102 printERROR "Insufficient Arguments."
103 return 1
104 fi
105
106 RESPONSE=""
107 DEF_ARG="$2"
108
109 while :
110 do
111 printf "$1 ? "
112 if [ -n "$DEF_ARG" ] ; then
113 printf "[$DEF_ARG] "
114 fi
115
116 read RESPONSE
117
118 if [ -n "$RESPONSE" ] ; then
119 break
120 elif [ -z "$RESPONSE" -a -n "$DEF_ARG" ] ; then
121 RESPONSE="$DEF_ARG"
122 break
123 fi
124 done
125
126 export RESPONSE
127 unset DEF_ARG
128 return 0
129 }
130
131 ################################################
132 # Name: getSpaceFree
133 # Desc: output the space avail for a directory
134 # Args: $1 -> The directory to check
135 ################################################
136
137 getSpaceFree() {
138
139 if [ $# -lt 1 ] ; then
140 printERROR "Insufficient Arguments."
141 return 1
142 fi
143
144 df -k "$1" | awk `NR != 1 { print $4 ; }'
145 }
146
147 ################################################
148 # Name: getSpaceUsed
149 # Desc: output the space used for a directory
150 # Args: $1 -> The directory to check
151 ################################################
152
153 getSpaceUsed() {
154
155 if [ $# -lt 1 ] ; then
156 printERROR "Insufficient Arguments."
157 return 1
158 fi
159
160 if [ ! -d "$1" ] ; then
161 printERROR "$1 is not a directory."
162 return 1
163 fi
164
165 du -sk "$1" | awk `{ print $1 ; }'
166 }
167
168 ################################################
169 # Name: getPID
170 # Desc: outputs a list of process id matching $1
171 # Args: $1 -> the command name to look for
172 ################################################
173
174 getPID() {
175
176 if [ $# -lt 1 ] ; then
177 printERROR "Insufficient Arguments."
178 return 1
179 fi
180
181 PSOPTS="-ef"
182
183 /bin/ps $PSOPTS | grep "$1" | grep -v grep | awk
[return]'{ print $2; }'
184 }
185
186 ################################################
187 # Name: getUID
188 # Desc: outputs a numeric user id
189 # Args: $1 -> a user name (optional)
190 ################################################
191
192 getUID() {
193 id $1 | sed -e 's/(.*$//' -e 's/^uid=//'
194 }
195
| Previous | Table of Contents | Next |