| 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 #!/bin/bash |
| 127 | 2 # recurse.sh |
| 128 | 3 |
| 129 | 4 # Can a script recursively call itself? |
| 130 | 5 # Yes, but is this of any practical use? |
| 131 | 6 # (See the following.) |
| 132 | 7 |
| 133 | 8 RANGE=10 |
| 134 | 9 MAXVAL=9 |
| 135 | 10 |
| 136 | 11 i=$RANDOM |
| 137 | 12 let "i %= $RANGE" # Generate a random number between 0 and $RANGE - 1. |
| 138 | 13 |
| 139 | 14 if [ "$i" -lt "$MAXVAL" ] |
| 140 | 15 then |
| 141 | 16 echo "i = $i" |
| 142 | 17 ./$0 # Script recursively spawns a new instance of itself. |
| 143 | 18 fi # Each child script does the same, until |
| 144 | 19 #+ a generated $i equals $MAXVAL. |
| 145 | 20 |
| 146 | 21 # Using a "while" loop instead of an "if/then" test causes problems. |
| 147 | 22 # Explain why. |
| 148 | 23 |
| 149 | 24 exit 0 |
| 150 | 25 |
| 151 | 26 # Note: |
| 152 | 27 # ---- |
| 153 | 28 # This script must have execute permission for it to work properly. |
| 154 | 29 # This is the case even if it is invoked by an "sh" command. |
| 155 | 30 # 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 #!/bin/bash |
| 178 | 2 # pb.sh: phone book |
| 179 | 3 |
| 180 | 4 # Written by Rick Boivie, and used with permission. |
| 181 | 5 # Modifications by ABS Guide author. |
| 182 | 6 |
| 183 | 7 MINARGS=1 # Script needs at least one argument. |
| 184 | 8 DATAFILE=./phonebook |
| 185 | 9 # A data file in current working directory |
| 186 | 10 #+ named "phonebook" must exist. |
| 187 | 11 PROGNAME=$0 |
| 188 | 12 E_NOARGS=70 # No arguments error. |
| 189 | 13 |
| 190 | 14 if [ $# -lt $MINARGS ]; then |
| 191 | 15 echo "Usage: "$PROGNAME" data-to-look-up" |
| 192 | 16 exit $E_NOARGS |
| 193 | 17 fi |
| 194 | 18 |
| 195 | 19 |
| 196 | 20 if [ $# -eq $MINARGS ]; then |
| 197 | 21 grep $1 "$DATAFILE" |
| 198 | 22 # 'grep' prints an error message if $DATAFILE not present. |
| 199 | 23 else |
| 200 | 24 ( shift; "$PROGNAME" $* ) | grep $1 |
| 201 | 25 # Script recursively calls itself. |
| 202 | 26 fi |
| 203 | 27 |
| 204 | 28 exit 0 # Script exits here. |
| 205 | 29 # Therefore, it's o.k. to put |
| 206 | 30 #+ non-hashmarked comments and data after this point. |
| 207 | 31 |
| 208 | 32 # ------------------------------------------------------------------------ |
| 209 | 33 Sample "phonebook" datafile: |
| 210 | 34 |
| 211 | 35 John Doe 1555 Main St., Baltimore, MD 21228 (410) 222-3333 |
| 212 | 36 Mary Moe 9899 Jones Blvd., Warren, NH 03787 (603) 898-3232 |
| 213 | 37 Richard Roe 856 E. 7th St., New York, NY 10009 (212) 333-4567 |
| 214 | 38 Sam Roe 956 E. 8th St., New York, NY 10009 (212) 444-5678 |
| 215 | 39 Zoe Zenobia 4481 N. Baker St., San Francisco, SF 94338 (415) 501-1631 |
| 216 | 40 # ------------------------------------------------------------------------ |
| 217 | 41 |
| 218 | 42 $bash pb.sh Roe |
| 219 | 43 Richard Roe 856 E. 7th St., New York, NY 10009 (212) 333-4567 |
| 220 | 44 Sam Roe 956 E. 8th St., New York, NY 10009 (212) 444-5678 |
| 221 | 45 |
| 222 | 46 $bash pb.sh Roe Sam |
| 223 | 47 Sam Roe 956 E. 8th St., New York, NY 10009 (212) 444-5678 |
| 224 | 48 |
| 225 | 49 # When more than one argument is passed to this script, |
| 226 | 50 #+ 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 #!/bin/bash |
| 249 | 2 # usrmnt.sh, written by Anthony Richardson |
| 250 | 3 # Used in ABS Guide with permission. |
| 251 | 4 |
| 252 | 5 # usage: usrmnt.sh |
| 253 | 6 # description: mount device, invoking user must be listed in the |
| 254 | 7 # MNTUSERS group in the /etc/sudoers file. |
| 255 | 8 |
| 256 | 9 # ---------------------------------------------------------- |
| 257 | 10 # This is a usermount script that reruns itself using sudo. |
| 258 | 11 # A user with the proper permissions only has to type |
| 259 | 12 |
| 260 | 13 # usermount /dev/fd0 /mnt/floppy |
| 261 | 14 |
| 262 | 15 # instead of |
| 263 | 16 |
| 264 | 17 # sudo usermount /dev/fd0 /mnt/floppy |
| 265 | 18 |
| 266 | 19 # I use this same technique for all of my |
| 267 | 20 #+ sudo scripts, because I find it convenient. |
| 268 | 21 # ---------------------------------------------------------- |
| 269 | 22 |
| 270 | 23 # If SUDO_COMMAND variable is not set we are not being run through |
| 271 | 24 #+ sudo, so rerun ourselves. Pass the user's real and group id . . . |
| 272 | 25 |
| 273 | 26 if [ -z "$SUDO_COMMAND" ] |
| 274 | 27 then |
| 275 | 28 mntusr=$(id -u) grpusr=$(id -g) sudo $0 $* |
| 276 | 29 exit 0 |
| 277 | 30 fi |
| 278 | 31 |
| 279 | 32 # We will only get here if we are being run by sudo. |
| 280 | 33 /bin/mount $* -o uid=$mntusr,gid=$grpusr |
| 281 | 34 |
| 282 | 35 exit 0 |
| 283 | 36 |
| 284 | 37 # Additional notes (from the author of this script): |
| 285 | 38 # ------------------------------------------------- |
| 286 | 39 |
| 287 | 40 # 1) Linux allows the "users" option in the /etc/fstab |
| 288 | 41 # file so that any user can mount removable media. |
| 289 | 42 # But, on a server, I like to allow only a few |
| 290 | 43 # individuals access to removable media. |
| 291 | 44 # I find using sudo gives me more control. |
| 292 | 45 |
| 293 | 46 # 2) I also find sudo to be more convenient than |
| 294 | 47 # accomplishing this task through groups. |
| 295 | 48 |
| 296 | 49 # 3) This method gives anyone with proper permissions |
| 297 | 50 # root access to the mount command, so be careful |
| 298 | 51 # about who you allow access. |
| 299 | 52 # You can get finer control over which access can be mounted |
| 300 | 53 # by using this same technique in separate mntfloppy, mntcdrom, |
| 301 | 54 # 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 | > |