Debian Codegraph @

HTML/recursionsct.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Recursion: a script calling itself</TITLE
6 ><META
7 NAME="GENERATOR"
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
9 "><LINK
10 REL="HOME"
11 TITLE="Advanced Bash-Scripting Guide"
12 HREF="index.html"><LINK
13 REL="UP"
14 TITLE="Miscellany"
15 HREF="miscellany.html"><LINK
16 REL="PREVIOUS"
17 TITLE="Tests and Comparisons: Alternatives"
18 HREF="testsandcomparisons.html"><LINK
19 REL="NEXT"
20 TITLE="Colorizing Scripts"
21 HREF="colorizing.html"><META
22 HTTP-EQUIV="Content-Style-Type"
23 CONTENT="text/css"><LINK
24 REL="stylesheet"
25 HREF="common/kde-common.css"
26 TYPE="text/css"><META
27 HTTP-EQUIV="Content-Type"
28 CONTENT="text/html; charset=iso-8859-1"><META
29 HTTP-EQUIV="Content-Language"
30 CONTENT="en"><LINK
31 REL="stylesheet"
32 HREF="common/kde-localised.css"
33 TYPE="text/css"
34 TITLE="KDE-English"><LINK
35 REL="stylesheet"
36 HREF="common/kde-default.css"
37 TYPE="text/css"
38 TITLE="KDE-Default"></HEAD
39 ><BODY
40 CLASS="SECT1"
41 BGCOLOR="#FFFFFF"
42 TEXT="#000000"
43 LINK="#AA0000"
44 VLINK="#AA0055"
45 ALINK="#AA0000"
46 STYLE="font-family: sans-serif;"
47 ><DIV
48 CLASS="NAVHEADER"
49 ><TABLE
50 SUMMARY="Header navigation table"
51 WIDTH="100%"
52 BORDER="0"
53 CELLPADDING="0"
54 CELLSPACING="0"
55 ><TR
56 ><TH
57 COLSPAN="3"
58 ALIGN="center"
59 >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
60 ></TR
61 ><TR
62 ><TD
63 WIDTH="10%"
64 ALIGN="left"
65 VALIGN="bottom"
66 ><A
67 HREF="testsandcomparisons.html"
68 ACCESSKEY="P"
69 >Prev</A
70 ></TD
71 ><TD
72 WIDTH="80%"
73 ALIGN="center"
74 VALIGN="bottom"
75 >Chapter 36. Miscellany</TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="bottom"
80 ><A
81 HREF="colorizing.html"
82 ACCESSKEY="N"
83 >Next</A
84 ></TD
85 ></TR
86 ></TABLE
87 ><HR
88 ALIGN="LEFT"
89 WIDTH="100%"></DIV
90 ><DIV
91 CLASS="SECT1"
92 ><H1
93 CLASS="SECT1"
94 ><A
95 NAME="RECURSIONSCT"
96 ></A
97 >36.4. Recursion: a script calling itself</H1
98 ><P
99 ><A
100 NAME="SCRIPTRECURSION"
101 ></A
102 ></P
103 ><P
104 >Can a script <A
105 HREF="localvar.html#RECURSIONREF"
106 >recursively</A
107 >
108 call itself? Indeed.</P
109 ><DIV
110 CLASS="EXAMPLE"
111 ><HR><A
112 NAME="RECURSE"
113 ></A
114 ><P
115 ><B
116 >Example 36-10. A (useless) script that recursively calls itself</B
117 ></P
118 ><TABLE
119 BORDER="0"
120 BGCOLOR="#E0E0E0"
121 WIDTH="100%"
122 ><TR
123 ><TD
124 ><PRE
125 CLASS="PROGRAMLISTING"
126 > 1&nbsp;#!/bin/bash
127 2&nbsp;# recurse.sh
128 3&nbsp;
129 4&nbsp;# Can a script recursively call itself?
130 5&nbsp;# Yes, but is this of any practical use?
131 6&nbsp;# (See the following.)
132 7&nbsp;
133 8&nbsp;RANGE=10
134 9&nbsp;MAXVAL=9
135 10&nbsp;
136 11&nbsp;i=$RANDOM
137 12&nbsp;let "i %= $RANGE" # Generate a random number between 0 and $RANGE - 1.
138 13&nbsp;
139 14&nbsp;if [ "$i" -lt "$MAXVAL" ]
140 15&nbsp;then
141 16&nbsp; echo "i = $i"
142 17&nbsp; ./$0 # Script recursively spawns a new instance of itself.
143 18&nbsp;fi # Each child script does the same, until
144 19&nbsp; #+ a generated $i equals $MAXVAL.
145 20&nbsp;
146 21&nbsp;# Using a "while" loop instead of an "if/then" test causes problems.
147 22&nbsp;# Explain why.
148 23&nbsp;
149 24&nbsp;exit 0
150 25&nbsp;
151 26&nbsp;# Note:
152 27&nbsp;# ----
153 28&nbsp;# This script must have execute permission for it to work properly.
154 29&nbsp;# This is the case even if it is invoked by an "sh" command.
155 30&nbsp;# Explain why.</PRE
156 ></TD
157 ></TR
158 ></TABLE
159 ><HR></DIV
160 ><DIV
161 CLASS="EXAMPLE"
162 ><HR><A
163 NAME="PBOOK"
164 ></A
165 ><P
166 ><B
167 >Example 36-11. A (useful) script that recursively calls itself</B
168 ></P
169 ><TABLE
170 BORDER="0"
171 BGCOLOR="#E0E0E0"
172 WIDTH="100%"
173 ><TR
174 ><TD
175 ><PRE
176 CLASS="PROGRAMLISTING"
177 > 1&nbsp;#!/bin/bash
178 2&nbsp;# pb.sh: phone book
179 3&nbsp;
180 4&nbsp;# Written by Rick Boivie, and used with permission.
181 5&nbsp;# Modifications by ABS Guide author.
182 6&nbsp;
183 7&nbsp;MINARGS=1 # Script needs at least one argument.
184 8&nbsp;DATAFILE=./phonebook
185 9&nbsp; # A data file in current working directory
186 10&nbsp; #+ named "phonebook" must exist.
187 11&nbsp;PROGNAME=$0
188 12&nbsp;E_NOARGS=70 # No arguments error.
189 13&nbsp;
190 14&nbsp;if [ $# -lt $MINARGS ]; then
191 15&nbsp; echo "Usage: "$PROGNAME" data-to-look-up"
192 16&nbsp; exit $E_NOARGS
193 17&nbsp;fi
194 18&nbsp;
195 19&nbsp;
196 20&nbsp;if [ $# -eq $MINARGS ]; then
197 21&nbsp; grep $1 "$DATAFILE"
198 22&nbsp; # 'grep' prints an error message if $DATAFILE not present.
199 23&nbsp;else
200 24&nbsp; ( shift; "$PROGNAME" $* ) | grep $1
201 25&nbsp; # Script recursively calls itself.
202 26&nbsp;fi
203 27&nbsp;
204 28&nbsp;exit 0 # Script exits here.
205 29&nbsp; # Therefore, it's o.k. to put
206 30&nbsp; #+ non-hashmarked comments and data after this point.
207 31&nbsp;
208 32&nbsp;# ------------------------------------------------------------------------
209 33&nbsp;Sample "phonebook" datafile:
210 34&nbsp;
211 35&nbsp;John Doe 1555 Main St., Baltimore, MD 21228 (410) 222-3333
212 36&nbsp;Mary Moe 9899 Jones Blvd., Warren, NH 03787 (603) 898-3232
213 37&nbsp;Richard Roe 856 E. 7th St., New York, NY 10009 (212) 333-4567
214 38&nbsp;Sam Roe 956 E. 8th St., New York, NY 10009 (212) 444-5678
215 39&nbsp;Zoe Zenobia 4481 N. Baker St., San Francisco, SF 94338 (415) 501-1631
216 40&nbsp;# ------------------------------------------------------------------------
217 41&nbsp;
218 42&nbsp;$bash pb.sh Roe
219 43&nbsp;Richard Roe 856 E. 7th St., New York, NY 10009 (212) 333-4567
220 44&nbsp;Sam Roe 956 E. 8th St., New York, NY 10009 (212) 444-5678
221 45&nbsp;
222 46&nbsp;$bash pb.sh Roe Sam
223 47&nbsp;Sam Roe 956 E. 8th St., New York, NY 10009 (212) 444-5678
224 48&nbsp;
225 49&nbsp;# When more than one argument is passed to this script,
226 50&nbsp;#+ it prints *only* the line(s) containing all the arguments.</PRE
227 ></TD
228 ></TR
229 ></TABLE
230 ><HR></DIV
231 ><DIV
232 CLASS="EXAMPLE"
233 ><HR><A
234 NAME="USRMNT"
235 ></A
236 ><P
237 ><B
238 >Example 36-12. Another (useful) script that recursively calls itself</B
239 ></P
240 ><TABLE
241 BORDER="0"
242 BGCOLOR="#E0E0E0"
243 WIDTH="100%"
244 ><TR
245 ><TD
246 ><PRE
247 CLASS="PROGRAMLISTING"
248 > 1&nbsp;#!/bin/bash
249 2&nbsp;# usrmnt.sh, written by Anthony Richardson
250 3&nbsp;# Used in ABS Guide with permission.
251 4&nbsp;
252 5&nbsp;# usage: usrmnt.sh
253 6&nbsp;# description: mount device, invoking user must be listed in the
254 7&nbsp;# MNTUSERS group in the /etc/sudoers file.
255 8&nbsp;
256 9&nbsp;# ----------------------------------------------------------
257 10&nbsp;# This is a usermount script that reruns itself using sudo.
258 11&nbsp;# A user with the proper permissions only has to type
259 12&nbsp;
260 13&nbsp;# usermount /dev/fd0 /mnt/floppy
261 14&nbsp;
262 15&nbsp;# instead of
263 16&nbsp;
264 17&nbsp;# sudo usermount /dev/fd0 /mnt/floppy
265 18&nbsp;
266 19&nbsp;# I use this same technique for all of my
267 20&nbsp;#+ sudo scripts, because I find it convenient.
268 21&nbsp;# ----------------------------------------------------------
269 22&nbsp;
270 23&nbsp;# If SUDO_COMMAND variable is not set we are not being run through
271 24&nbsp;#+ sudo, so rerun ourselves. Pass the user's real and group id . . .
272 25&nbsp;
273 26&nbsp;if [ -z "$SUDO_COMMAND" ]
274 27&nbsp;then
275 28&nbsp; mntusr=$(id -u) grpusr=$(id -g) sudo $0 $*
276 29&nbsp; exit 0
277 30&nbsp;fi
278 31&nbsp;
279 32&nbsp;# We will only get here if we are being run by sudo.
280 33&nbsp;/bin/mount $* -o uid=$mntusr,gid=$grpusr
281 34&nbsp;
282 35&nbsp;exit 0
283 36&nbsp;
284 37&nbsp;# Additional notes (from the author of this script):
285 38&nbsp;# -------------------------------------------------
286 39&nbsp;
287 40&nbsp;# 1) Linux allows the "users" option in the /etc/fstab
288 41&nbsp;# file so that any user can mount removable media.
289 42&nbsp;# But, on a server, I like to allow only a few
290 43&nbsp;# individuals access to removable media.
291 44&nbsp;# I find using sudo gives me more control.
292 45&nbsp;
293 46&nbsp;# 2) I also find sudo to be more convenient than
294 47&nbsp;# accomplishing this task through groups.
295 48&nbsp;
296 49&nbsp;# 3) This method gives anyone with proper permissions
297 50&nbsp;# root access to the mount command, so be careful
298 51&nbsp;# about who you allow access.
299 52&nbsp;# You can get finer control over which access can be mounted
300 53&nbsp;# by using this same technique in separate mntfloppy, mntcdrom,
301 54&nbsp;# and mntsamba scripts.</PRE
302 ></TD
303 ></TR
304 ></TABLE
305 ><HR></DIV
306 ><DIV
307 CLASS="CAUTION"
308 ><TABLE
309 CLASS="CAUTION"
310 WIDTH="100%"
311 BORDER="0"
312 ><TR
313 ><TD
314 WIDTH="25"
315 ALIGN="CENTER"
316 VALIGN="TOP"
317 ><IMG
318 SRC="common/caution.png"
319 HSPACE="5"
320 ALT="Caution"></TD
321 ><TD
322 ALIGN="LEFT"
323 VALIGN="TOP"
324 ><P
325 >Too many levels of recursion can exhaust the
326 script's stack space, causing a segfault.</P
327 ></TD
328 ></TR
329 ></TABLE
330 ></DIV
331 ></DIV
332 ><DIV
333 CLASS="NAVFOOTER"
334 ><HR
335 ALIGN="LEFT"
336 WIDTH="100%"><TABLE
337 SUMMARY="Footer navigation table"
338 WIDTH="100%"
339 BORDER="0"
340 CELLPADDING="0"
341 CELLSPACING="0"
342 ><TR
343 ><TD
344 WIDTH="33%"
345 ALIGN="left"
346 VALIGN="top"
347 ><A
348 HREF="testsandcomparisons.html"
349 ACCESSKEY="P"
350 >Prev</A
351 ></TD
352 ><TD
353 WIDTH="34%"
354 ALIGN="center"
355 VALIGN="top"
356 ><A
357 HREF="index.html"
358 ACCESSKEY="H"
359 >Home</A
360 ></TD
361 ><TD
362 WIDTH="33%"
363 ALIGN="right"
364 VALIGN="top"
365 ><A
366 HREF="colorizing.html"
367 ACCESSKEY="N"
368 >Next</A
369 ></TD
370 ></TR
371 ><TR
372 ><TD
373 WIDTH="33%"
374 ALIGN="left"
375 VALIGN="top"
376 >Tests and Comparisons: Alternatives</TD
377 ><TD
378 WIDTH="34%"
379 ALIGN="center"
380 VALIGN="top"
381 ><A
382 HREF="miscellany.html"
383 ACCESSKEY="U"
384 >Up</A
385 ></TD
386 ><TD
387 WIDTH="33%"
388 ALIGN="right"
389 VALIGN="top"
390 ><SPAN
391 CLASS="QUOTE"
392 >"Colorizing"</SPAN
393 > Scripts</TD
394 ></TR
395 ></TABLE
396 ></DIV
397 ></BODY
398 ></HTML
399 >