| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <HTML |
| 3 | ><HEAD |
| 4 | ><TITLE |
| 5 | >Functions</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="Advanced Topics" |
| 15 | HREF="part5.html"><LINK |
| 16 | REL="PREVIOUS" |
| 17 | TITLE="Process Substitution" |
| 18 | HREF="process-sub.html"><LINK |
| 19 | REL="NEXT" |
| 20 | TITLE="Local Variables" |
| 21 | HREF="localvar.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="CHAPTER" |
| 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="process-sub.html" |
| 68 | ACCESSKEY="P" |
| 69 | >Prev</A |
| 70 | ></TD |
| 71 | ><TD |
| 72 | WIDTH="80%" |
| 73 | ALIGN="center" |
| 74 | VALIGN="bottom" |
| 75 | ></TD |
| 76 | ><TD |
| 77 | WIDTH="10%" |
| 78 | ALIGN="right" |
| 79 | VALIGN="bottom" |
| 80 | ><A |
| 81 | HREF="localvar.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="CHAPTER" |
| 92 | ><H1 |
| 93 | ><A |
| 94 | NAME="FUNCTIONS" |
| 95 | ></A |
| 96 | >Chapter 24. Functions</H1 |
| 97 | ><P |
| 98 | ><A |
| 99 | NAME="FUNCTIONREF" |
| 100 | ></A |
| 101 | ></P |
| 102 | ><P |
| 103 | >Like <SPAN |
| 104 | CLASS="QUOTE" |
| 105 | >"real"</SPAN |
| 106 | > programming languages, |
| 107 | Bash has functions, though in a somewhat limited implementation. |
| 108 | A function is a subroutine, a <A |
| 109 | HREF="special-chars.html#CODEBLOCKREF" |
| 110 | >code |
| 111 | block</A |
| 112 | > that implements a set of operations, a <SPAN |
| 113 | CLASS="QUOTE" |
| 114 | >"black |
| 115 | box"</SPAN |
| 116 | > that performs a specified task. Wherever there is |
| 117 | repetitive code, when a task repeats with only slight variations in |
| 118 | procedure, then consider using a function.</P |
| 119 | ><P |
| 120 | ><P |
| 121 | ><B |
| 122 | CLASS="COMMAND" |
| 123 | >function</B |
| 124 | > <TT |
| 125 | CLASS="REPLACEABLE" |
| 126 | ><I |
| 127 | >function_name</I |
| 128 | ></TT |
| 129 | > { <BR> <TT |
| 130 | CLASS="REPLACEABLE" |
| 131 | ><I |
| 132 | >command</I |
| 133 | ></TT |
| 134 | >... <BR> } <BR></P |
| 135 | > |
| 136 | or |
| 137 | <P |
| 138 | > <TT |
| 139 | CLASS="REPLACEABLE" |
| 140 | ><I |
| 141 | >function_name</I |
| 142 | ></TT |
| 143 | > () { <BR> <TT |
| 144 | CLASS="REPLACEABLE" |
| 145 | ><I |
| 146 | >command</I |
| 147 | ></TT |
| 148 | >... <BR> } <BR></P |
| 149 | > |
| 150 | </P |
| 151 | ><P |
| 152 | >This second form will cheer the hearts of C programmers |
| 153 | (and is more <A |
| 154 | HREF="portabilityissues.html" |
| 155 | >portable</A |
| 156 | >).</P |
| 157 | ><P |
| 158 | >As in C, the function's opening bracket may optionally appear |
| 159 | on the second line.</P |
| 160 | ><P |
| 161 | ><P |
| 162 | > <TT |
| 163 | CLASS="REPLACEABLE" |
| 164 | ><I |
| 165 | >function_name</I |
| 166 | ></TT |
| 167 | > () <BR> { <BR> <TT |
| 168 | CLASS="REPLACEABLE" |
| 169 | ><I |
| 170 | >command</I |
| 171 | ></TT |
| 172 | >... <BR> } <BR></P |
| 173 | > |
| 174 | </P |
| 175 | ><DIV |
| 176 | CLASS="NOTE" |
| 177 | ><TABLE |
| 178 | CLASS="NOTE" |
| 179 | WIDTH="100%" |
| 180 | BORDER="0" |
| 181 | ><TR |
| 182 | ><TD |
| 183 | WIDTH="25" |
| 184 | ALIGN="CENTER" |
| 185 | VALIGN="TOP" |
| 186 | ><IMG |
| 187 | SRC="common/note.png" |
| 188 | HSPACE="5" |
| 189 | ALT="Note"></TD |
| 190 | ><TD |
| 191 | ALIGN="LEFT" |
| 192 | VALIGN="TOP" |
| 193 | ><P |
| 194 | >A function may be <SPAN |
| 195 | CLASS="QUOTE" |
| 196 | >"compacted"</SPAN |
| 197 | > into a single |
| 198 | line.</P |
| 199 | ><P |
| 200 | ><TABLE |
| 201 | BORDER="0" |
| 202 | BGCOLOR="#E0E0E0" |
| 203 | WIDTH="100%" |
| 204 | ><TR |
| 205 | ><TD |
| 206 | ><PRE |
| 207 | CLASS="PROGRAMLISTING" |
| 208 | > 1 fun () { echo "This is a function"; echo; } |
| 209 | 2 # ^ ^</PRE |
| 210 | ></TD |
| 211 | ></TR |
| 212 | ></TABLE |
| 213 | ></P |
| 214 | ><P |
| 215 | >In this case, however, a <I |
| 216 | CLASS="FIRSTTERM" |
| 217 | >semicolon</I |
| 218 | > |
| 219 | must follow the final command in the function.</P |
| 220 | ><P |
| 221 | ><TABLE |
| 222 | BORDER="0" |
| 223 | BGCOLOR="#E0E0E0" |
| 224 | WIDTH="100%" |
| 225 | ><TR |
| 226 | ><TD |
| 227 | ><PRE |
| 228 | CLASS="PROGRAMLISTING" |
| 229 | > 1 fun () { echo "This is a function"; echo } # Error! |
| 230 | 2 # ^ |
| 231 | 3 |
| 232 | 4 fun2 () { echo "Even a single-command function? Yes!"; } |
| 233 | 5 # ^</PRE |
| 234 | ></TD |
| 235 | ></TR |
| 236 | ></TABLE |
| 237 | ></P |
| 238 | ></TD |
| 239 | ></TR |
| 240 | ></TABLE |
| 241 | ></DIV |
| 242 | ><P |
| 243 | >Functions are called, <I |
| 244 | CLASS="FIRSTTERM" |
| 245 | >triggered</I |
| 246 | >, simply by |
| 247 | invoking their names. <SPAN |
| 248 | CLASS="emphasis" |
| 249 | ><I |
| 250 | CLASS="EMPHASIS" |
| 251 | >A function call is equivalent to |
| 252 | a command.</I |
| 253 | ></SPAN |
| 254 | ></P |
| 255 | ><DIV |
| 256 | CLASS="EXAMPLE" |
| 257 | ><HR><A |
| 258 | NAME="EX59" |
| 259 | ></A |
| 260 | ><P |
| 261 | ><B |
| 262 | >Example 24-1. Simple functions</B |
| 263 | ></P |
| 264 | ><TABLE |
| 265 | BORDER="0" |
| 266 | BGCOLOR="#E0E0E0" |
| 267 | WIDTH="100%" |
| 268 | ><TR |
| 269 | ><TD |
| 270 | ><PRE |
| 271 | CLASS="PROGRAMLISTING" |
| 272 | > 1 #!/bin/bash |
| 273 | 2 # ex59.sh: Exercising functions (simple). |
| 274 | 3 |
| 275 | 4 JUST_A_SECOND=1 |
| 276 | 5 |
| 277 | 6 funky () |
| 278 | 7 { # This is about as simple as functions get. |
| 279 | 8 echo "This is a funky function." |
| 280 | 9 echo "Now exiting funky function." |
| 281 | 10 } # Function declaration must precede call. |
| 282 | 11 |
| 283 | 12 |
| 284 | 13 fun () |
| 285 | 14 { # A somewhat more complex function. |
| 286 | 15 i=0 |
| 287 | 16 REPEATS=30 |
| 288 | 17 |
| 289 | 18 echo |
| 290 | 19 echo "And now the fun really begins." |
| 291 | 20 echo |
| 292 | 21 |
| 293 | 22 sleep $JUST_A_SECOND # Hey, wait a second! |
| 294 | 23 while [ $i -lt $REPEATS ] |
| 295 | 24 do |
| 296 | 25 echo "----------FUNCTIONS---------->" |
| 297 | 26 echo "<------------ARE-------------" |
| 298 | 27 echo "<------------FUN------------>" |
| 299 | 28 echo |
| 300 | 29 let "i+=1" |
| 301 | 30 done |
| 302 | 31 } |
| 303 | 32 |
| 304 | 33 # Now, call the functions. |
| 305 | 34 |
| 306 | 35 funky |
| 307 | 36 fun |
| 308 | 37 |
| 309 | 38 exit $?</PRE |
| 310 | ></TD |
| 311 | ></TR |
| 312 | ></TABLE |
| 313 | ><HR></DIV |
| 314 | ><P |
| 315 | ><A |
| 316 | NAME="FUNCTDEFMUST" |
| 317 | ></A |
| 318 | ></P |
| 319 | ><P |
| 320 | >The function definition must precede the first call to |
| 321 | it. There is no method of <SPAN |
| 322 | CLASS="QUOTE" |
| 323 | >"declaring"</SPAN |
| 324 | > the function, |
| 325 | as, for example, in C. |
| 326 | <TABLE |
| 327 | BORDER="0" |
| 328 | BGCOLOR="#E0E0E0" |
| 329 | WIDTH="100%" |
| 330 | ><TR |
| 331 | ><TD |
| 332 | ><PRE |
| 333 | CLASS="PROGRAMLISTING" |
| 334 | > 1 f1 |
| 335 | 2 # Will give an error message, since function "f1" not yet defined. |
| 336 | 3 |
| 337 | 4 declare -f f1 # This doesn't help either. |
| 338 | 5 f1 # Still an error message. |
| 339 | 6 |
| 340 | 7 # However... |
| 341 | 8 |
| 342 | 9 |
| 343 | 10 f1 () |
| 344 | 11 { |
| 345 | 12 echo "Calling function \"f2\" from within function \"f1\"." |
| 346 | 13 f2 |
| 347 | 14 } |
| 348 | 15 |
| 349 | 16 f2 () |
| 350 | 17 { |
| 351 | 18 echo "Function \"f2\"." |
| 352 | 19 } |
| 353 | 20 |
| 354 | 21 f1 # Function "f2" is not actually called until this point, |
| 355 | 22 #+ although it is referenced before its definition. |
| 356 | 23 # This is permissible. |
| 357 | 24 |
| 358 | 25 # Thanks, S.C.</PRE |
| 359 | ></TD |
| 360 | ></TR |
| 361 | ></TABLE |
| 362 | > |
| 363 | </P |
| 364 | ><DIV |
| 365 | CLASS="NOTE" |
| 366 | ><TABLE |
| 367 | CLASS="NOTE" |
| 368 | WIDTH="100%" |
| 369 | BORDER="0" |
| 370 | ><TR |
| 371 | ><TD |
| 372 | WIDTH="25" |
| 373 | ALIGN="CENTER" |
| 374 | VALIGN="TOP" |
| 375 | ><IMG |
| 376 | SRC="common/note.png" |
| 377 | HSPACE="5" |
| 378 | ALT="Note"></TD |
| 379 | ><TD |
| 380 | ALIGN="LEFT" |
| 381 | VALIGN="TOP" |
| 382 | ><P |
| 383 | ><A |
| 384 | NAME="EMPTYFUNC" |
| 385 | ></A |
| 386 | >Functions may not be empty! |
| 387 | <TABLE |
| 388 | BORDER="0" |
| 389 | BGCOLOR="#E0E0E0" |
| 390 | WIDTH="100%" |
| 391 | ><TR |
| 392 | ><TD |
| 393 | ><PRE |
| 394 | CLASS="PROGRAMLISTING" |
| 395 | > 1 #!/bin/bash |
| 396 | 2 # empty-function.sh |
| 397 | 3 |
| 398 | 4 empty () |
| 399 | 5 { |
| 400 | 6 } |
| 401 | 7 |
| 402 | 8 exit 0 # Will not exit here! |
| 403 | 9 |
| 404 | 10 # $ sh empty-function.sh |
| 405 | 11 # empty-function.sh: line 6: syntax error near unexpected token `}' |
| 406 | 12 # empty-function.sh: line 6: `}' |
| 407 | 13 |
| 408 | 14 # $ echo $? |
| 409 | 15 # 2 |
| 410 | 16 |
| 411 | 17 |
| 412 | 18 # Note that a function containing only comments is empty. |
| 413 | 19 |
| 414 | 20 func () |
| 415 | 21 { |
| 416 | 22 # Comment 1. |
| 417 | 23 # Comment 2. |
| 418 | 24 # This is still an empty function. |
| 419 | 25 # Thank you, Mark Bova, for pointing this out. |
| 420 | 26 } |
| 421 | 27 # Results in same error message as above. |
| 422 | 28 |
| 423 | 29 |
| 424 | 30 # However ... |
| 425 | 31 |
| 426 | 32 not_quite_empty () |
| 427 | 33 { |
| 428 | 34 illegal_command |
| 429 | 35 } # A script containing this function will *not* bomb |
| 430 | 36 #+ as long as the function is not called. |
| 431 | 37 |
| 432 | 38 not_empty () |
| 433 | 39 { |
| 434 | 40 : |
| 435 | 41 } # Contains a : (null command), and this is okay. |
| 436 | 42 |
| 437 | 43 |
| 438 | 44 # Thank you, Dominick Geyer and Thiemo Kellner.</PRE |
| 439 | ></TD |
| 440 | ></TR |
| 441 | ></TABLE |
| 442 | ></P |
| 443 | ></TD |
| 444 | ></TR |
| 445 | ></TABLE |
| 446 | ></DIV |
| 447 | ><P |
| 448 | >It is even possible to nest a function within another function, |
| 449 | although this is not very useful. |
| 450 | <TABLE |
| 451 | BORDER="0" |
| 452 | BGCOLOR="#E0E0E0" |
| 453 | WIDTH="100%" |
| 454 | ><TR |
| 455 | ><TD |
| 456 | ><PRE |
| 457 | CLASS="PROGRAMLISTING" |
| 458 | > 1 f1 () |
| 459 | 2 { |
| 460 | 3 |
| 461 | 4 f2 () # nested |
| 462 | 5 { |
| 463 | 6 echo "Function \"f2\", inside \"f1\"." |
| 464 | 7 } |
| 465 | 8 |
| 466 | 9 } |
| 467 | 10 |
| 468 | 11 f2 # Gives an error message. |
| 469 | 12 # Even a preceding "declare -f f2" wouldn't help. |
| 470 | 13 |
| 471 | 14 echo |
| 472 | 15 |
| 473 | 16 f1 # Does nothing, since calling "f1" does not automatically call "f2". |
| 474 | 17 f2 # Now, it's all right to call "f2", |
| 475 | 18 #+ since its definition has been made visible by calling "f1". |
| 476 | 19 |
| 477 | 20 # Thanks, S.C.</PRE |
| 478 | ></TD |
| 479 | ></TR |
| 480 | ></TABLE |
| 481 | > |
| 482 | </P |
| 483 | ><P |
| 484 | >Function declarations can appear in unlikely places, even where a |
| 485 | command would otherwise go. |
| 486 | <TABLE |
| 487 | BORDER="0" |
| 488 | BGCOLOR="#E0E0E0" |
| 489 | WIDTH="100%" |
| 490 | ><TR |
| 491 | ><TD |
| 492 | ><PRE |
| 493 | CLASS="PROGRAMLISTING" |
| 494 | > 1 ls -l | foo() { echo "foo"; } # Permissible, but useless. |
| 495 | 2 |
| 496 | 3 |
| 497 | 4 |
| 498 | 5 if [ "$USER" = bozo ] |
| 499 | 6 then |
| 500 | 7 bozo_greet () # Function definition embedded in an if/then construct. |
| 501 | 8 { |
| 502 | 9 echo "Hello, Bozo." |
| 503 | 10 } |
| 504 | 11 fi |
| 505 | 12 |
| 506 | 13 bozo_greet # Works only for Bozo, and other users get an error. |
| 507 | 14 |
| 508 | 15 |
| 509 | 16 |
| 510 | 17 # Something like this might be useful in some contexts. |
| 511 | 18 NO_EXIT=1 # Will enable function definition below. |
| 512 | 19 |
| 513 | 20 [[ $NO_EXIT -eq 1 ]] && exit() { true; } # Function definition in an "and-list". |
| 514 | 21 # If $NO_EXIT is 1, declares "exit ()". |
| 515 | 22 # This disables the "exit" builtin by aliasing it to "true". |
| 516 | 23 |
| 517 | 24 exit # Invokes "exit ()" function, not "exit" builtin. |
| 518 | 25 |
| 519 | 26 |
| 520 | 27 |
| 521 | 28 # Or, similarly: |
| 522 | 29 filename=file1 |
| 523 | 30 |
| 524 | 31 [ -f "$filename" ] && |
| 525 | 32 foo () { rm -f "$filename"; echo "File "$filename" deleted."; } || |
| 526 | 33 foo () { echo "File "$filename" not found."; touch bar; } |
| 527 | 34 |
| 528 | 35 foo |
| 529 | 36 |
| 530 | 37 # Thanks, S.C. and Christopher Head</PRE |
| 531 | ></TD |
| 532 | ></TR |
| 533 | ></TABLE |
| 534 | > |
| 535 | </P |
| 536 | ><P |
| 537 | ><A |
| 538 | NAME="FSTRANGEREF" |
| 539 | ></A |
| 540 | >Function names can take strange |
| 541 | forms. |
| 542 | <TABLE |
| 543 | BORDER="0" |
| 544 | BGCOLOR="#E0E0E0" |
| 545 | WIDTH="100%" |
| 546 | ><TR |
| 547 | ><TD |
| 548 | ><PRE |
| 549 | CLASS="PROGRAMLISTING" |
| 550 | > 1 _(){ for i in {1..10}; do echo -n "$FUNCNAME"; done; echo; } |
| 551 | 2 # ^^^ No space between function name and parentheses. |
| 552 | 3 # This doesn't always work. Why not? |
| 553 | 4 |
| 554 | 5 # Now, let's invoke the function. |
| 555 | 6 _ # __________ |
| 556 | 7 # ^^^^^^^^^^ 10 underscores (10 x function name)! |
| 557 | 8 # A "naked" underscore is an acceptable function name. |
| 558 | 9 |
| 559 | 10 |
| 560 | 11 # In fact, a colon is likewise an acceptable function name. |
| 561 | 12 |
| 562 | 13 :(){ echo ":"; }; : |
| 563 | 14 |
| 564 | 15 # Of what use is this? |
| 565 | 16 # It's a devious way to obfuscate the code in a script.</PRE |
| 566 | ></TD |
| 567 | ></TR |
| 568 | ></TABLE |
| 569 | > |
| 570 | See also <A |
| 571 | HREF="contributed-scripts.html#GRONSFELD" |
| 572 | >Example A-56</A |
| 573 | ></P |
| 574 | ><DIV |
| 575 | CLASS="NOTE" |
| 576 | ><TABLE |
| 577 | CLASS="NOTE" |
| 578 | WIDTH="100%" |
| 579 | BORDER="0" |
| 580 | ><TR |
| 581 | ><TD |
| 582 | WIDTH="25" |
| 583 | ALIGN="CENTER" |
| 584 | VALIGN="TOP" |
| 585 | ><IMG |
| 586 | SRC="common/note.png" |
| 587 | HSPACE="5" |
| 588 | ALT="Note"></TD |
| 589 | ><TD |
| 590 | ALIGN="LEFT" |
| 591 | VALIGN="TOP" |
| 592 | ><P |
| 593 | >What happens when different versions of the same function |
| 594 | appear in a script? |
| 595 | <TABLE |
| 596 | BORDER="0" |
| 597 | BGCOLOR="#E0E0E0" |
| 598 | WIDTH="100%" |
| 599 | ><TR |
| 600 | ><TD |
| 601 | ><PRE |
| 602 | CLASS="PROGRAMLISTING" |
| 603 | > 1 # As Yan Chen points out, |
| 604 | 2 # when a function is defined multiple times, |
| 605 | 3 # the final version is what is invoked. |
| 606 | 4 # This is not, however, particularly useful. |
| 607 | 5 |
| 608 | 6 func () |
| 609 | 7 { |
| 610 | 8 echo "First version of func ()." |
| 611 | 9 } |
| 612 | 10 |
| 613 | 11 func () |
| 614 | 12 { |
| 615 | 13 echo "Second version of func ()." |
| 616 | 14 } |
| 617 | 15 |
| 618 | 16 func # Second version of func (). |
| 619 | 17 |
| 620 | 18 exit $? |
| 621 | 19 |
| 622 | 20 # It is even possible to use functions to override |
| 623 | 21 #+ or preempt system commands. |
| 624 | 22 # Of course, this is *not* advisable.</PRE |
| 625 | ></TD |
| 626 | ></TR |
| 627 | ></TABLE |
| 628 | ></P |
| 629 | ></TD |
| 630 | ></TR |
| 631 | ></TABLE |
| 632 | ></DIV |
| 633 | ><DIV |
| 634 | CLASS="SECT1" |
| 635 | ><H1 |
| 636 | CLASS="SECT1" |
| 637 | ><A |
| 638 | NAME="COMPLEXFUNCT" |
| 639 | ></A |
| 640 | >24.1. Complex Functions and Function Complexities</H1 |
| 641 | ><P |
| 642 | >Functions may process arguments passed to them and return |
| 643 | an <A |
| 644 | HREF="exit-status.html#EXITSTATUSREF" |
| 645 | >exit status</A |
| 646 | > to the script |
| 647 | for further processing.</P |
| 648 | ><TABLE |
| 649 | BORDER="0" |
| 650 | BGCOLOR="#E0E0E0" |
| 651 | WIDTH="100%" |
| 652 | ><TR |
| 653 | ><TD |
| 654 | ><PRE |
| 655 | CLASS="PROGRAMLISTING" |
| 656 | > 1 function_name $arg1 $arg2</PRE |
| 657 | ></TD |
| 658 | ></TR |
| 659 | ></TABLE |
| 660 | ><P |
| 661 | ><A |
| 662 | NAME="PASSEDARGS" |
| 663 | ></A |
| 664 | ></P |
| 665 | ><P |
| 666 | >The function refers to the passed arguments by position (as if they were |
| 667 | <A |
| 668 | HREF="variables2.html#POSPARAMREF" |
| 669 | >positional parameters</A |
| 670 | >), |
| 671 | that is, <TT |
| 672 | CLASS="VARNAME" |
| 673 | >$1</TT |
| 674 | >, <TT |
| 675 | CLASS="VARNAME" |
| 676 | >$2</TT |
| 677 | >, and |
| 678 | so forth.</P |
| 679 | ><DIV |
| 680 | CLASS="EXAMPLE" |
| 681 | ><HR><A |
| 682 | NAME="EX60" |
| 683 | ></A |
| 684 | ><P |
| 685 | ><B |
| 686 | >Example 24-2. Function Taking Parameters</B |
| 687 | ></P |
| 688 | ><TABLE |
| 689 | BORDER="0" |
| 690 | BGCOLOR="#E0E0E0" |
| 691 | WIDTH="100%" |
| 692 | ><TR |
| 693 | ><TD |
| 694 | ><PRE |
| 695 | CLASS="PROGRAMLISTING" |
| 696 | > 1 #!/bin/bash |
| 697 | 2 # Functions and parameters |
| 698 | 3 |
| 699 | 4 DEFAULT=default # Default param value. |
| 700 | 5 |
| 701 | 6 func2 () { |
| 702 | 7 if [ -z "$1" ] # Is parameter #1 zero length? |
| 703 | 8 then |
| 704 | 9 echo "-Parameter #1 is zero length.-" # Or no parameter passed. |
| 705 | 10 else |
| 706 | 11 echo "-Parameter #1 is \"$1\".-" |
| 707 | 12 fi |
| 708 | 13 |
| 709 | 14 variable=${1-$DEFAULT} # What does |
| 710 | 15 echo "variable = $variable" #+ parameter substitution show? |
| 711 | 16 # --------------------------- |
| 712 | 17 # It distinguishes between |
| 713 | 18 #+ no param and a null param. |
| 714 | 19 |
| 715 | 20 if [ "$2" ] |
| 716 | 21 then |
| 717 | 22 echo "-Parameter #2 is \"$2\".-" |
| 718 | 23 fi |
| 719 | 24 |
| 720 | 25 return 0 |
| 721 | 26 } |
| 722 | 27 |
| 723 | 28 echo |
| 724 | 29 |
| 725 | 30 echo "Nothing passed." |
| 726 | 31 func2 # Called with no params |
| 727 | 32 echo |
| 728 | 33 |
| 729 | 34 |
| 730 | 35 echo "Zero-length parameter passed." |
| 731 | 36 func2 "" # Called with zero-length param |
| 732 | 37 echo |
| 733 | 38 |
| 734 | 39 echo "Null parameter passed." |
| 735 | 40 func2 "$uninitialized_param" # Called with uninitialized param |
| 736 | 41 echo |
| 737 | 42 |
| 738 | 43 echo "One parameter passed." |
| 739 | 44 func2 first # Called with one param |
| 740 | 45 echo |
| 741 | 46 |
| 742 | 47 echo "Two parameters passed." |
| 743 | 48 func2 first second # Called with two params |
| 744 | 49 echo |
| 745 | 50 |
| 746 | 51 echo "\"\" \"second\" passed." |
| 747 | 52 func2 "" second # Called with zero-length first parameter |
| 748 | 53 echo # and ASCII string as a second one. |
| 749 | 54 |
| 750 | 55 exit 0</PRE |
| 751 | ></TD |
| 752 | ></TR |
| 753 | ></TABLE |
| 754 | ><HR></DIV |
| 755 | ><P |
| 756 | ><A |
| 757 | NAME="FSHIFTREF" |
| 758 | ></A |
| 759 | ></P |
| 760 | ><DIV |
| 761 | CLASS="IMPORTANT" |
| 762 | ><TABLE |
| 763 | CLASS="IMPORTANT" |
| 764 | WIDTH="100%" |
| 765 | BORDER="0" |
| 766 | ><TR |
| 767 | ><TD |
| 768 | WIDTH="25" |
| 769 | ALIGN="CENTER" |
| 770 | VALIGN="TOP" |
| 771 | ><IMG |
| 772 | SRC="common/important.png" |
| 773 | HSPACE="5" |
| 774 | ALT="Important"></TD |
| 775 | ><TD |
| 776 | ALIGN="LEFT" |
| 777 | VALIGN="TOP" |
| 778 | ><P |
| 779 | >The <A |
| 780 | HREF="othertypesv.html#SHIFTREF" |
| 781 | >shift</A |
| 782 | > |
| 783 | command works on arguments passed to functions (see <A |
| 784 | HREF="assortedtips.html#MULTIPLICATION" |
| 785 | >Example 36-18</A |
| 786 | >).</P |
| 787 | ></TD |
| 788 | ></TR |
| 789 | ></TABLE |
| 790 | ></DIV |
| 791 | ><P |
| 792 | >But, what about command-line arguments passed to the script? |
| 793 | Does a function see them? Well, let's clear up the confusion.</P |
| 794 | ><DIV |
| 795 | CLASS="EXAMPLE" |
| 796 | ><HR><A |
| 797 | NAME="FUNCCMDLINEARG" |
| 798 | ></A |
| 799 | ><P |
| 800 | ><B |
| 801 | >Example 24-3. Functions and command-line args passed to the script</B |
| 802 | ></P |
| 803 | ><TABLE |
| 804 | BORDER="0" |
| 805 | BGCOLOR="#E0E0E0" |
| 806 | WIDTH="100%" |
| 807 | ><TR |
| 808 | ><TD |
| 809 | ><PRE |
| 810 | CLASS="PROGRAMLISTING" |
| 811 | > 1 #!/bin/bash |
| 812 | 2 # func-cmdlinearg.sh |
| 813 | 3 # Call this script with a command-line argument, |
| 814 | 4 #+ something like $0 arg1. |
| 815 | 5 |
| 816 | 6 |
| 817 | 7 func () |
| 818 | 8 |
| 819 | 9 { |
| 820 | 10 echo "$1" # Echoes first arg passed to the function. |
| 821 | 11 } # Does a command-line arg qualify? |
| 822 | 12 |
| 823 | 13 echo "First call to function: no arg passed." |
| 824 | 14 echo "See if command-line arg is seen." |
| 825 | 15 func |
| 826 | 16 # No! Command-line arg not seen. |
| 827 | 17 |
| 828 | 18 echo "============================================================" |
| 829 | 19 echo |
| 830 | 20 echo "Second call to function: command-line arg passed explicitly." |
| 831 | 21 func $1 |
| 832 | 22 # Now it's seen! |
| 833 | 23 |
| 834 | 24 exit 0</PRE |
| 835 | ></TD |
| 836 | ></TR |
| 837 | ></TABLE |
| 838 | ><HR></DIV |
| 839 | ><P |
| 840 | >In contrast to certain other programming languages, |
| 841 | shell scripts normally pass only value parameters to |
| 842 | functions. Variable names (which are actually |
| 843 | <I |
| 844 | CLASS="FIRSTTERM" |
| 845 | >pointers</I |
| 846 | >), if |
| 847 | passed as parameters to functions, will be treated as string |
| 848 | literals. <SPAN |
| 849 | CLASS="emphasis" |
| 850 | ><I |
| 851 | CLASS="EMPHASIS" |
| 852 | >Functions interpret their arguments |
| 853 | literally.</I |
| 854 | ></SPAN |
| 855 | ></P |
| 856 | ><P |
| 857 | ><A |
| 858 | NAME="FUNCPOINTERS" |
| 859 | ></A |
| 860 | ></P |
| 861 | ><P |
| 862 | ><A |
| 863 | HREF="ivr.html#IVRREF" |
| 864 | >Indirect variable |
| 865 | references</A |
| 866 | > (see <A |
| 867 | HREF="bash2.html#EX78" |
| 868 | >Example 37-2</A |
| 869 | >) provide a clumsy |
| 870 | sort of mechanism for passing variable pointers to |
| 871 | functions.</P |
| 872 | ><DIV |
| 873 | CLASS="EXAMPLE" |
| 874 | ><HR><A |
| 875 | NAME="INDFUNC" |
| 876 | ></A |
| 877 | ><P |
| 878 | ><B |
| 879 | >Example 24-4. Passing an indirect reference to a function</B |
| 880 | ></P |
| 881 | ><TABLE |
| 882 | BORDER="0" |
| 883 | BGCOLOR="#E0E0E0" |
| 884 | WIDTH="100%" |
| 885 | ><TR |
| 886 | ><TD |
| 887 | ><PRE |
| 888 | CLASS="PROGRAMLISTING" |
| 889 | > 1 #!/bin/bash |
| 890 | 2 # ind-func.sh: Passing an indirect reference to a function. |
| 891 | 3 |
| 892 | 4 echo_var () |
| 893 | 5 { |
| 894 | 6 echo "$1" |
| 895 | 7 } |
| 896 | 8 |
| 897 | 9 message=Hello |
| 898 | 10 Hello=Goodbye |
| 899 | 11 |
| 900 | 12 echo_var "$message" # Hello |
| 901 | 13 # Now, let's pass an indirect reference to the function. |
| 902 | 14 echo_var "${!message}" # Goodbye |
| 903 | 15 |
| 904 | 16 echo "-------------" |
| 905 | 17 |
| 906 | 18 # What happens if we change the contents of "hello" variable? |
| 907 | 19 Hello="Hello, again!" |
| 908 | 20 echo_var "$message" # Hello |
| 909 | 21 echo_var "${!message}" # Hello, again! |
| 910 | 22 |
| 911 | 23 exit 0</PRE |
| 912 | ></TD |
| 913 | ></TR |
| 914 | ></TABLE |
| 915 | ><HR></DIV |
| 916 | ><P |
| 917 | >The next logical question is whether parameters can be |
| 918 | dereferenced <SPAN |
| 919 | CLASS="emphasis" |
| 920 | ><I |
| 921 | CLASS="EMPHASIS" |
| 922 | >after</I |
| 923 | ></SPAN |
| 924 | > being passed to a |
| 925 | function.</P |
| 926 | ><DIV |
| 927 | CLASS="EXAMPLE" |
| 928 | ><HR><A |
| 929 | NAME="DEREFERENCECL" |
| 930 | ></A |
| 931 | ><P |
| 932 | ><B |
| 933 | >Example 24-5. Dereferencing a parameter passed to a function</B |
| 934 | ></P |
| 935 | ><TABLE |
| 936 | BORDER="0" |
| 937 | BGCOLOR="#E0E0E0" |
| 938 | WIDTH="100%" |
| 939 | ><TR |
| 940 | ><TD |
| 941 | ><PRE |
| 942 | CLASS="PROGRAMLISTING" |
| 943 | > 1 #!/bin/bash |
| 944 | 2 # dereference.sh |
| 945 | 3 # Dereferencing parameter passed to a function. |
| 946 | 4 # Script by Bruce W. Clare. |
| 947 | 5 |
| 948 | 6 dereference () |
| 949 | 7 { |
| 950 | 8 y=\$"$1" # Name of variable (not value!). |
| 951 | 9 echo $y # $Junk |
| 952 | 10 |
| 953 | 11 x=`eval "expr \"$y\" "` |
| 954 | 12 echo $1=$x |
| 955 | 13 eval "$1=\"Some Different Text \"" # Assign new value. |
| 956 | 14 } |
| 957 | 15 |
| 958 | 16 Junk="Some Text" |
| 959 | 17 echo $Junk "before" # Some Text before |
| 960 | 18 |
| 961 | 19 dereference Junk |
| 962 | 20 echo $Junk "after" # Some Different Text after |
| 963 | 21 |
| 964 | 22 exit 0</PRE |
| 965 | ></TD |
| 966 | ></TR |
| 967 | ></TABLE |
| 968 | ><HR></DIV |
| 969 | ><DIV |
| 970 | CLASS="EXAMPLE" |
| 971 | ><HR><A |
| 972 | NAME="REFPARAMS" |
| 973 | ></A |
| 974 | ><P |
| 975 | ><B |
| 976 | >Example 24-6. Again, dereferencing a parameter passed to a function</B |
| 977 | ></P |
| 978 | ><TABLE |
| 979 | BORDER="0" |
| 980 | BGCOLOR="#E0E0E0" |
| 981 | WIDTH="100%" |
| 982 | ><TR |
| 983 | ><TD |
| 984 | ><PRE |
| 985 | CLASS="PROGRAMLISTING" |
| 986 | > 1 #!/bin/bash |
| 987 | 2 # ref-params.sh: Dereferencing a parameter passed to a function. |
| 988 | 3 # (Complex Example) |
| 989 | 4 |
| 990 | 5 ITERATIONS=3 # How many times to get input. |
| 991 | 6 icount=1 |
| 992 | 7 |
| 993 | 8 my_read () { |
| 994 | 9 # Called with my_read varname, |
| 995 | 10 #+ outputs the previous value between brackets as the default value, |
| 996 | 11 #+ then asks for a new value. |
| 997 | 12 |
| 998 | 13 local local_var |
| 999 | 14 |
| 1000 | 15 echo -n "Enter a value " |
| 1001 | 16 eval 'echo -n "[$'$1'] "' # Previous value. |
| 1002 | 17 # eval echo -n "[\$$1] " # Easier to understand, |
| 1003 | 18 #+ but loses trailing space in user prompt. |
| 1004 | 19 read local_var |
| 1005 | 20 [ -n "$local_var" ] && eval $1=\$local_var |
| 1006 | 21 |
| 1007 | 22 # "And-list": if "local_var" then set "$1" to its value. |
| 1008 | 23 } |
| 1009 | 24 |
| 1010 | 25 echo |
| 1011 | 26 |
| 1012 | 27 while [ "$icount" -le "$ITERATIONS" ] |
| 1013 | 28 do |
| 1014 | 29 my_read var |
| 1015 | 30 echo "Entry #$icount = $var" |
| 1016 | 31 let "icount += 1" |
| 1017 | 32 echo |
| 1018 | 33 done |
| 1019 | 34 |
| 1020 | 35 |
| 1021 | 36 # Thanks to Stephane Chazelas for providing this instructive example. |
| 1022 | 37 |
| 1023 | 38 exit 0</PRE |
| 1024 | ></TD |
| 1025 | ></TR |
| 1026 | ></TABLE |
| 1027 | ><HR></DIV |
| 1028 | ><DIV |
| 1029 | CLASS="VARIABLELIST" |
| 1030 | ><P |
| 1031 | ><B |
| 1032 | ><A |
| 1033 | NAME="EXITRETURN1" |
| 1034 | ></A |
| 1035 | >Exit and Return</B |
| 1036 | ></P |
| 1037 | ><DL |
| 1038 | ><DT |
| 1039 | ><B |
| 1040 | CLASS="COMMAND" |
| 1041 | >exit status</B |
| 1042 | ></DT |
| 1043 | ><DD |
| 1044 | ><P |
| 1045 | >Functions return a value, called an <I |
| 1046 | CLASS="FIRSTTERM" |
| 1047 | >exit |
| 1048 | status</I |
| 1049 | >. This is analogous to the <A |
| 1050 | HREF="exit-status.html#EXITSTATUSREF" |
| 1051 | >exit status</A |
| 1052 | > returned by a |
| 1053 | command. The exit status may be explicitly specified |
| 1054 | by a <B |
| 1055 | CLASS="COMMAND" |
| 1056 | >return</B |
| 1057 | > statement, otherwise it |
| 1058 | is the exit status of the last command in the function |
| 1059 | (<SPAN |
| 1060 | CLASS="RETURNVALUE" |
| 1061 | >0</SPAN |
| 1062 | > if successful, and a non-zero |
| 1063 | error code if not). This <A |
| 1064 | HREF="exit-status.html#EXITSTATUSREF" |
| 1065 | >exit |
| 1066 | status</A |
| 1067 | > may be used in the script by referencing it |
| 1068 | as <A |
| 1069 | HREF="variables2.html#XSTATVARREF" |
| 1070 | >$?</A |
| 1071 | >. This mechanism |
| 1072 | effectively permits script functions to have a <SPAN |
| 1073 | CLASS="QUOTE" |
| 1074 | >"return |
| 1075 | value"</SPAN |
| 1076 | > similar to C functions.</P |
| 1077 | ></DD |
| 1078 | ><DT |
| 1079 | ><B |
| 1080 | CLASS="COMMAND" |
| 1081 | >return</B |
| 1082 | ></DT |
| 1083 | ><DD |
| 1084 | ><P |
| 1085 | ><A |
| 1086 | NAME="RETURNREF" |
| 1087 | ></A |
| 1088 | ></P |
| 1089 | ><P |
| 1090 | >Terminates a function. A <B |
| 1091 | CLASS="COMMAND" |
| 1092 | >return</B |
| 1093 | > command |
| 1094 | <A |
| 1095 | NAME="AEN18474" |
| 1096 | HREF="#FTN.AEN18474" |
| 1097 | >[1]</A |
| 1098 | > |
| 1099 | optionally takes an <I |
| 1100 | CLASS="FIRSTTERM" |
| 1101 | >integer</I |
| 1102 | > |
| 1103 | argument, which is returned to the calling script as |
| 1104 | the <SPAN |
| 1105 | CLASS="QUOTE" |
| 1106 | >"exit status"</SPAN |
| 1107 | > of the function, and |
| 1108 | this exit status is assigned to the variable <A |
| 1109 | HREF="variables2.html#XSTATVARREF" |
| 1110 | >$?</A |
| 1111 | >.</P |
| 1112 | ><DIV |
| 1113 | CLASS="EXAMPLE" |
| 1114 | ><HR><A |
| 1115 | NAME="MAX" |
| 1116 | ></A |
| 1117 | ><P |
| 1118 | ><B |
| 1119 | >Example 24-7. Maximum of two numbers</B |
| 1120 | ></P |
| 1121 | ><TABLE |
| 1122 | BORDER="0" |
| 1123 | BGCOLOR="#E0E0E0" |
| 1124 | WIDTH="90%" |
| 1125 | ><TR |
| 1126 | ><TD |
| 1127 | ><PRE |
| 1128 | CLASS="PROGRAMLISTING" |
| 1129 | > 1 #!/bin/bash |
| 1130 | 2 # max.sh: Maximum of two integers. |
| 1131 | 3 |
| 1132 | 4 E_PARAM_ERR=250 # If less than 2 params passed to function. |
| 1133 | 5 EQUAL=251 # Return value if both params equal. |
| 1134 | 6 # Error values out of range of any |
| 1135 | 7 #+ params that might be fed to the function. |
| 1136 | 8 |
| 1137 | 9 max2 () # Returns larger of two numbers. |
| 1138 | 10 { # Note: numbers compared must be less than 250. |
| 1139 | 11 if [ -z "$2" ] |
| 1140 | 12 then |
| 1141 | 13 return $E_PARAM_ERR |
| 1142 | 14 fi |
| 1143 | 15 |
| 1144 | 16 if [ "$1" -eq "$2" ] |
| 1145 | 17 then |
| 1146 | 18 return $EQUAL |
| 1147 | 19 else |
| 1148 | 20 if [ "$1" -gt "$2" ] |
| 1149 | 21 then |
| 1150 | 22 return $1 |
| 1151 | 23 else |
| 1152 | 24 return $2 |
| 1153 | 25 fi |
| 1154 | 26 fi |
| 1155 | 27 } |
| 1156 | 28 |
| 1157 | 29 max2 33 34 |
| 1158 | 30 return_val=$? |
| 1159 | 31 |
| 1160 | 32 if [ "$return_val" -eq $E_PARAM_ERR ] |
| 1161 | 33 then |
| 1162 | 34 echo "Need to pass two parameters to the function." |
| 1163 | 35 elif [ "$return_val" -eq $EQUAL ] |
| 1164 | 36 then |
| 1165 | 37 echo "The two numbers are equal." |
| 1166 | 38 else |
| 1167 | 39 echo "The larger of the two numbers is $return_val." |
| 1168 | 40 fi |
| 1169 | 41 |
| 1170 | 42 |
| 1171 | 43 exit 0 |
| 1172 | 44 |
| 1173 | 45 # Exercise (easy): |
| 1174 | 46 # --------------- |
| 1175 | 47 # Convert this to an interactive script, |
| 1176 | 48 #+ that is, have the script ask for input (two numbers).</PRE |
| 1177 | ></TD |
| 1178 | ></TR |
| 1179 | ></TABLE |
| 1180 | ><HR></DIV |
| 1181 | ><DIV |
| 1182 | CLASS="TIP" |
| 1183 | ><TABLE |
| 1184 | CLASS="TIP" |
| 1185 | WIDTH="90%" |
| 1186 | BORDER="0" |
| 1187 | ><TR |
| 1188 | ><TD |
| 1189 | WIDTH="25" |
| 1190 | ALIGN="CENTER" |
| 1191 | VALIGN="TOP" |
| 1192 | ><IMG |
| 1193 | SRC="common/tip.png" |
| 1194 | HSPACE="5" |
| 1195 | ALT="Tip"></TD |
| 1196 | ><TD |
| 1197 | ALIGN="LEFT" |
| 1198 | VALIGN="TOP" |
| 1199 | ><P |
| 1200 | >For a function to return a string or array, use a |
| 1201 | dedicated variable. |
| 1202 | <TABLE |
| 1203 | BORDER="0" |
| 1204 | BGCOLOR="#E0E0E0" |
| 1205 | WIDTH="90%" |
| 1206 | ><TR |
| 1207 | ><TD |
| 1208 | ><PRE |
| 1209 | CLASS="PROGRAMLISTING" |
| 1210 | > 1 count_lines_in_etc_passwd() |
| 1211 | 2 { |
| 1212 | 3 [[ -r /etc/passwd ]] && REPLY=$(echo $(wc -l < /etc/passwd)) |
| 1213 | 4 # If /etc/passwd is readable, set REPLY to line count. |
| 1214 | 5 # Returns both a parameter value and status information. |
| 1215 | 6 # The 'echo' seems unnecessary, but . . . |
| 1216 | 7 #+ it removes excess whitespace from the output. |
| 1217 | 8 } |
| 1218 | 9 |
| 1219 | 10 if count_lines_in_etc_passwd |
| 1220 | 11 then |
| 1221 | 12 echo "There are $REPLY lines in /etc/passwd." |
| 1222 | 13 else |
| 1223 | 14 echo "Cannot count lines in /etc/passwd." |
| 1224 | 15 fi |
| 1225 | 16 |
| 1226 | 17 # Thanks, S.C.</PRE |
| 1227 | ></TD |
| 1228 | ></TR |
| 1229 | ></TABLE |
| 1230 | > |
| 1231 | </P |
| 1232 | ></TD |
| 1233 | ></TR |
| 1234 | ></TABLE |
| 1235 | ></DIV |
| 1236 | ><DIV |
| 1237 | CLASS="EXAMPLE" |
| 1238 | ><HR><A |
| 1239 | NAME="EX61" |
| 1240 | ></A |
| 1241 | ><P |
| 1242 | ><B |
| 1243 | >Example 24-8. Converting numbers to Roman numerals</B |
| 1244 | ></P |
| 1245 | ><TABLE |
| 1246 | BORDER="0" |
| 1247 | BGCOLOR="#E0E0E0" |
| 1248 | WIDTH="90%" |
| 1249 | ><TR |
| 1250 | ><TD |
| 1251 | ><PRE |
| 1252 | CLASS="PROGRAMLISTING" |
| 1253 | > 1 #!/bin/bash |
| 1254 | 2 |
| 1255 | 3 # Arabic number to Roman numeral conversion |
| 1256 | 4 # Range: 0 - 200 |
| 1257 | 5 # It's crude, but it works. |
| 1258 | 6 |
| 1259 | 7 # Extending the range and otherwise improving the script is left as an exercise. |
| 1260 | 8 |
| 1261 | 9 # Usage: roman number-to-convert |
| 1262 | 10 |
| 1263 | 11 LIMIT=200 |
| 1264 | 12 E_ARG_ERR=65 |
| 1265 | 13 E_OUT_OF_RANGE=66 |
| 1266 | 14 |
| 1267 | 15 if [ -z "$1" ] |
| 1268 | 16 then |
| 1269 | 17 echo "Usage: `basename $0` number-to-convert" |
| 1270 | 18 exit $E_ARG_ERR |
| 1271 | 19 fi |
| 1272 | 20 |
| 1273 | 21 num=$1 |
| 1274 | 22 if [ "$num" -gt $LIMIT ] |
| 1275 | 23 then |
| 1276 | 24 echo "Out of range!" |
| 1277 | 25 exit $E_OUT_OF_RANGE |
| 1278 | 26 fi |
| 1279 | 27 |
| 1280 | 28 to_roman () # Must declare function before first call to it. |
| 1281 | 29 { |
| 1282 | 30 number=$1 |
| 1283 | 31 factor=$2 |
| 1284 | 32 rchar=$3 |
| 1285 | 33 let "remainder = number - factor" |
| 1286 | 34 while [ "$remainder" -ge 0 ] |
| 1287 | 35 do |
| 1288 | 36 echo -n $rchar |
| 1289 | 37 let "number -= factor" |
| 1290 | 38 let "remainder = number - factor" |
| 1291 | 39 done |
| 1292 | 40 |
| 1293 | 41 return $number |
| 1294 | 42 # Exercises: |
| 1295 | 43 # --------- |
| 1296 | 44 # 1) Explain how this function works. |
| 1297 | 45 # Hint: division by successive subtraction. |
| 1298 | 46 # 2) Extend to range of the function. |
| 1299 | 47 # Hint: use "echo" and command-substitution capture. |
| 1300 | 48 } |
| 1301 | 49 |
| 1302 | 50 |
| 1303 | 51 to_roman $num 100 C |
| 1304 | 52 num=$? |
| 1305 | 53 to_roman $num 90 LXXXX |
| 1306 | 54 num=$? |
| 1307 | 55 to_roman $num 50 L |
| 1308 | 56 num=$? |
| 1309 | 57 to_roman $num 40 XL |
| 1310 | 58 num=$? |
| 1311 | 59 to_roman $num 10 X |
| 1312 | 60 num=$? |
| 1313 | 61 to_roman $num 9 IX |
| 1314 | 62 num=$? |
| 1315 | 63 to_roman $num 5 V |
| 1316 | 64 num=$? |
| 1317 | 65 to_roman $num 4 IV |
| 1318 | 66 num=$? |
| 1319 | 67 to_roman $num 1 I |
| 1320 | 68 # Successive calls to conversion function! |
| 1321 | 69 # Is this really necessary??? Can it be simplified? |
| 1322 | 70 |
| 1323 | 71 echo |
| 1324 | 72 |
| 1325 | 73 exit</PRE |
| 1326 | ></TD |
| 1327 | ></TR |
| 1328 | ></TABLE |
| 1329 | ><HR></DIV |
| 1330 | ><P |
| 1331 | >See also <A |
| 1332 | HREF="testbranch.html#ISALPHA" |
| 1333 | >Example 11-29</A |
| 1334 | >.</P |
| 1335 | ><DIV |
| 1336 | CLASS="IMPORTANT" |
| 1337 | ><TABLE |
| 1338 | CLASS="IMPORTANT" |
| 1339 | WIDTH="90%" |
| 1340 | BORDER="0" |
| 1341 | ><TR |
| 1342 | ><TD |
| 1343 | WIDTH="25" |
| 1344 | ALIGN="CENTER" |
| 1345 | VALIGN="TOP" |
| 1346 | ><IMG |
| 1347 | SRC="common/important.png" |
| 1348 | HSPACE="5" |
| 1349 | ALT="Important"></TD |
| 1350 | ><TD |
| 1351 | ALIGN="LEFT" |
| 1352 | VALIGN="TOP" |
| 1353 | ><P |
| 1354 | >The largest positive integer a function can return is |
| 1355 | 255. The <B |
| 1356 | CLASS="COMMAND" |
| 1357 | >return</B |
| 1358 | > command is closely tied |
| 1359 | to the concept of <A |
| 1360 | HREF="exit-status.html#EXITSTATUSREF" |
| 1361 | >exit |
| 1362 | status</A |
| 1363 | >, which accounts for this particular |
| 1364 | limitation. Fortunately, there are various <A |
| 1365 | HREF="assortedtips.html#RVT" |
| 1366 | >workarounds</A |
| 1367 | > for those situations |
| 1368 | requiring a large integer return value from a |
| 1369 | function.</P |
| 1370 | ><DIV |
| 1371 | CLASS="EXAMPLE" |
| 1372 | ><HR><A |
| 1373 | NAME="RETURNTEST" |
| 1374 | ></A |
| 1375 | ><P |
| 1376 | ><B |
| 1377 | >Example 24-9. Testing large return values in a function</B |
| 1378 | ></P |
| 1379 | ><TABLE |
| 1380 | BORDER="0" |
| 1381 | BGCOLOR="#E0E0E0" |
| 1382 | WIDTH="90%" |
| 1383 | ><TR |
| 1384 | ><TD |
| 1385 | ><PRE |
| 1386 | CLASS="PROGRAMLISTING" |
| 1387 | > 1 #!/bin/bash |
| 1388 | 2 # return-test.sh |
| 1389 | 3 |
| 1390 | 4 # The largest positive value a function can return is 255. |
| 1391 | 5 |
| 1392 | 6 return_test () # Returns whatever passed to it. |
| 1393 | 7 { |
| 1394 | 8 return $1 |
| 1395 | 9 } |
| 1396 | 10 |
| 1397 | 11 return_test 27 # o.k. |
| 1398 | 12 echo $? # Returns 27. |
| 1399 | 13 |
| 1400 | 14 return_test 255 # Still o.k. |
| 1401 | 15 echo $? # Returns 255. |
| 1402 | 16 |
| 1403 | 17 return_test 257 # Error! |
| 1404 | 18 echo $? # Returns 1 (return code for miscellaneous error). |
| 1405 | 19 |
| 1406 | 20 # ========================================================= |
| 1407 | 21 return_test -151896 # Do large negative numbers work? |
| 1408 | 22 echo $? # Will this return -151896? |
| 1409 | 23 # No! It returns 168. |
| 1410 | 24 # Version of Bash before 2.05b permitted |
| 1411 | 25 #+ large negative integer return values. |
| 1412 | 26 # It happened to be a useful feature. |
| 1413 | 27 # Newer versions of Bash unfortunately plug this loophole. |
| 1414 | 28 # This may break older scripts. |
| 1415 | 29 # Caution! |
| 1416 | 30 # ========================================================= |
| 1417 | 31 |
| 1418 | 32 exit 0</PRE |
| 1419 | ></TD |
| 1420 | ></TR |
| 1421 | ></TABLE |
| 1422 | ><HR></DIV |
| 1423 | ><P |
| 1424 | >A workaround for obtaining large integer <SPAN |
| 1425 | CLASS="QUOTE" |
| 1426 | >"return |
| 1427 | values"</SPAN |
| 1428 | > is to simply assign the <SPAN |
| 1429 | CLASS="QUOTE" |
| 1430 | >"return |
| 1431 | value"</SPAN |
| 1432 | > to a global variable. |
| 1433 | |
| 1434 | <TABLE |
| 1435 | BORDER="0" |
| 1436 | BGCOLOR="#E0E0E0" |
| 1437 | WIDTH="90%" |
| 1438 | ><TR |
| 1439 | ><TD |
| 1440 | ><PRE |
| 1441 | CLASS="PROGRAMLISTING" |
| 1442 | > 1 Return_Val= # Global variable to hold oversize return value of function. |
| 1443 | 2 |
| 1444 | 3 alt_return_test () |
| 1445 | 4 { |
| 1446 | 5 fvar=$1 |
| 1447 | 6 Return_Val=$fvar |
| 1448 | 7 return # Returns 0 (success). |
| 1449 | 8 } |
| 1450 | 9 |
| 1451 | 10 alt_return_test 1 |
| 1452 | 11 echo $? # 0 |
| 1453 | 12 echo "return value = $Return_Val" # 1 |
| 1454 | 13 |
| 1455 | 14 alt_return_test 256 |
| 1456 | 15 echo "return value = $Return_Val" # 256 |
| 1457 | 16 |
| 1458 | 17 alt_return_test 257 |
| 1459 | 18 echo "return value = $Return_Val" # 257 |
| 1460 | 19 |
| 1461 | 20 alt_return_test 25701 |
| 1462 | 21 echo "return value = $Return_Val" #25701</PRE |
| 1463 | ></TD |
| 1464 | ></TR |
| 1465 | ></TABLE |
| 1466 | > |
| 1467 | </P |
| 1468 | ><P |
| 1469 | ><A |
| 1470 | NAME="CAPTURERETVAL" |
| 1471 | ></A |
| 1472 | ></P |
| 1473 | ><P |
| 1474 | >A more elegant method is to have the function |
| 1475 | <B |
| 1476 | CLASS="COMMAND" |
| 1477 | >echo</B |
| 1478 | > its <SPAN |
| 1479 | CLASS="QUOTE" |
| 1480 | >"return |
| 1481 | value to <TT |
| 1482 | CLASS="FILENAME" |
| 1483 | >stdout</TT |
| 1484 | >,"</SPAN |
| 1485 | > and |
| 1486 | then capture it by <A |
| 1487 | HREF="commandsub.html#COMMANDSUBREF" |
| 1488 | >command |
| 1489 | substitution</A |
| 1490 | >. See the <A |
| 1491 | HREF="assortedtips.html#RVT" |
| 1492 | >discussion |
| 1493 | of this</A |
| 1494 | > in <A |
| 1495 | HREF="assortedtips.html" |
| 1496 | >Section 36.7</A |
| 1497 | >.</P |
| 1498 | ><DIV |
| 1499 | CLASS="EXAMPLE" |
| 1500 | ><HR><A |
| 1501 | NAME="MAX2" |
| 1502 | ></A |
| 1503 | ><P |
| 1504 | ><B |
| 1505 | >Example 24-10. Comparing two large integers</B |
| 1506 | ></P |
| 1507 | ><TABLE |
| 1508 | BORDER="0" |
| 1509 | BGCOLOR="#E0E0E0" |
| 1510 | WIDTH="90%" |
| 1511 | ><TR |
| 1512 | ><TD |
| 1513 | ><PRE |
| 1514 | CLASS="PROGRAMLISTING" |
| 1515 | > 1 #!/bin/bash |
| 1516 | 2 # max2.sh: Maximum of two LARGE integers. |
| 1517 | 3 |
| 1518 | 4 # This is the previous "max.sh" example, |
| 1519 | 5 #+ modified to permit comparing large integers. |
| 1520 | 6 |
| 1521 | 7 EQUAL=0 # Return value if both params equal. |
| 1522 | 8 E_PARAM_ERR=-99999 # Not enough params passed to function. |
| 1523 | 9 # ^^^^^^ Out of range of any params that might be passed. |
| 1524 | 10 |
| 1525 | 11 max2 () # "Returns" larger of two numbers. |
| 1526 | 12 { |
| 1527 | 13 if [ -z "$2" ] |
| 1528 | 14 then |
| 1529 | 15 echo $E_PARAM_ERR |
| 1530 | 16 return |
| 1531 | 17 fi |
| 1532 | 18 |
| 1533 | 19 if [ "$1" -eq "$2" ] |
| 1534 | 20 then |
| 1535 | 21 echo $EQUAL |
| 1536 | 22 return |
| 1537 | 23 else |
| 1538 | 24 if [ "$1" -gt "$2" ] |
| 1539 | 25 then |
| 1540 | 26 retval=$1 |
| 1541 | 27 else |
| 1542 | 28 retval=$2 |
| 1543 | 29 fi |
| 1544 | 30 fi |
| 1545 | 31 |
| 1546 | 32 echo $retval # Echoes (to stdout), rather than returning value. |
| 1547 | 33 # Why? |
| 1548 | 34 } |
| 1549 | 35 |
| 1550 | 36 |
| 1551 | 37 return_val=$(max2 33001 33997) |
| 1552 | 38 # ^^^^ Function name |
| 1553 | 39 # ^^^^^ ^^^^^ Params passed |
| 1554 | 40 # This is actually a form of command substitution: |
| 1555 | 41 #+ treating a function as if it were a command, |
| 1556 | 42 #+ and assigning the stdout of the function to the variable "return_val." |
| 1557 | 43 |
| 1558 | 44 |
| 1559 | 45 # ========================= OUTPUT ======================== |
| 1560 | 46 if [ "$return_val" -eq "$E_PARAM_ERR" ] |
| 1561 | 47 then |
| 1562 | 48 echo "Error in parameters passed to comparison function!" |
| 1563 | 49 elif [ "$return_val" -eq "$EQUAL" ] |
| 1564 | 50 then |
| 1565 | 51 echo "The two numbers are equal." |
| 1566 | 52 else |
| 1567 | 53 echo "The larger of the two numbers is $return_val." |
| 1568 | 54 fi |
| 1569 | 55 # ========================================================= |
| 1570 | 56 |
| 1571 | 57 exit 0 |
| 1572 | 58 |
| 1573 | 59 # Exercises: |
| 1574 | 60 # --------- |
| 1575 | 61 # 1) Find a more elegant way of testing |
| 1576 | 62 #+ the parameters passed to the function. |
| 1577 | 63 # 2) Simplify the if/then structure at "OUTPUT." |
| 1578 | 64 # 3) Rewrite the script to take input from command-line parameters.</PRE |
| 1579 | ></TD |
| 1580 | ></TR |
| 1581 | ></TABLE |
| 1582 | ><HR></DIV |
| 1583 | ><P |
| 1584 | >Here is another example of capturing a function |
| 1585 | <SPAN |
| 1586 | CLASS="QUOTE" |
| 1587 | >"return value."</SPAN |
| 1588 | > Understanding it requires some |
| 1589 | knowledge of <A |
| 1590 | HREF="awk.html#AWKREF" |
| 1591 | >awk</A |
| 1592 | >. |
| 1593 | |
| 1594 | <TABLE |
| 1595 | BORDER="0" |
| 1596 | BGCOLOR="#E0E0E0" |
| 1597 | WIDTH="90%" |
| 1598 | ><TR |
| 1599 | ><TD |
| 1600 | ><PRE |
| 1601 | CLASS="PROGRAMLISTING" |
| 1602 | > 1 month_length () # Takes month number as an argument. |
| 1603 | 2 { # Returns number of days in month. |
| 1604 | 3 monthD="31 28 31 30 31 30 31 31 30 31 30 31" # Declare as local? |
| 1605 | 4 echo "$monthD" | awk '{ print $'"${1}"' }' # Tricky. |
| 1606 | 5 # ^^^^^^^^^ |
| 1607 | 6 # Parameter passed to function ($1 -- month number), then to awk. |
| 1608 | 7 # Awk sees this as "print $1 . . . print $12" (depending on month number) |
| 1609 | 8 # Template for passing a parameter to embedded awk script: |
| 1610 | 9 # $'"${script_parameter}"' |
| 1611 | 10 |
| 1612 | 11 # Here's a slightly simpler awk construct: |
| 1613 | 12 # echo $monthD | awk -v month=$1 '{print $(month)}' |
| 1614 | 13 # Uses the -v awk option, which assigns a variable value |
| 1615 | 14 #+ prior to execution of the awk program block. |
| 1616 | 15 # Thank you, Rich. |
| 1617 | 16 |
| 1618 | 17 # Needs error checking for correct parameter range (1-12) |
| 1619 | 18 #+ and for February in leap year. |
| 1620 | 19 } |
| 1621 | 20 |
| 1622 | 21 # ---------------------------------------------- |
| 1623 | 22 # Usage example: |
| 1624 | 23 month=4 # April, for example (4th month). |
| 1625 | 24 days_in=$(month_length $month) |
| 1626 | 25 echo $days_in # 30 |
| 1627 | 26 # ----------------------------------------------</PRE |
| 1628 | ></TD |
| 1629 | ></TR |
| 1630 | ></TABLE |
| 1631 | ></P |
| 1632 | ><P |
| 1633 | >See also <A |
| 1634 | HREF="contributed-scripts.html#DAYSBETWEEN" |
| 1635 | >Example A-7</A |
| 1636 | > |
| 1637 | and <A |
| 1638 | HREF="contributed-scripts.html#STDDEV" |
| 1639 | >Example A-37</A |
| 1640 | >.</P |
| 1641 | ><P |
| 1642 | ><TT |
| 1643 | CLASS="USERINPUT" |
| 1644 | ><B |
| 1645 | >Exercise:</B |
| 1646 | ></TT |
| 1647 | > Using what we have |
| 1648 | just learned, extend the previous <A |
| 1649 | HREF="functions.html#EX61" |
| 1650 | >Roman numerals example</A |
| 1651 | > to accept |
| 1652 | arbitrarily large input.</P |
| 1653 | ></TD |
| 1654 | ></TR |
| 1655 | ></TABLE |
| 1656 | ></DIV |
| 1657 | ></DD |
| 1658 | ></DL |
| 1659 | ></DIV |
| 1660 | ><DIV |
| 1661 | CLASS="VARIABLELIST" |
| 1662 | ><P |
| 1663 | ><B |
| 1664 | ><A |
| 1665 | NAME="REDSTDINFUNC1" |
| 1666 | ></A |
| 1667 | >Redirection</B |
| 1668 | ></P |
| 1669 | ><DL |
| 1670 | ><DT |
| 1671 | ><TT |
| 1672 | CLASS="REPLACEABLE" |
| 1673 | ><I |
| 1674 | >Redirecting the stdin |
| 1675 | of a function</I |
| 1676 | ></TT |
| 1677 | ></DT |
| 1678 | ><DD |
| 1679 | ><P |
| 1680 | >A function is essentially a <A |
| 1681 | HREF="special-chars.html#CODEBLOCKREF" |
| 1682 | >code block</A |
| 1683 | >, which means its |
| 1684 | <TT |
| 1685 | CLASS="FILENAME" |
| 1686 | >stdin</TT |
| 1687 | > can be redirected (as in <A |
| 1688 | HREF="special-chars.html#EX8" |
| 1689 | >Example 3-1</A |
| 1690 | >).</P |
| 1691 | ><DIV |
| 1692 | CLASS="EXAMPLE" |
| 1693 | ><HR><A |
| 1694 | NAME="REALNAME" |
| 1695 | ></A |
| 1696 | ><P |
| 1697 | ><B |
| 1698 | >Example 24-11. Real name from username</B |
| 1699 | ></P |
| 1700 | ><TABLE |
| 1701 | BORDER="0" |
| 1702 | BGCOLOR="#E0E0E0" |
| 1703 | WIDTH="90%" |
| 1704 | ><TR |
| 1705 | ><TD |
| 1706 | ><PRE |
| 1707 | CLASS="PROGRAMLISTING" |
| 1708 | > 1 #!/bin/bash |
| 1709 | 2 # realname.sh |
| 1710 | 3 # |
| 1711 | 4 # From username, gets "real name" from /etc/passwd. |
| 1712 | 5 |
| 1713 | 6 |
| 1714 | 7 ARGCOUNT=1 # Expect one arg. |
| 1715 | 8 E_WRONGARGS=85 |
| 1716 | 9 |
| 1717 | 10 file=/etc/passwd |
| 1718 | 11 pattern=$1 |
| 1719 | 12 |
| 1720 | 13 if [ $# -ne "$ARGCOUNT" ] |
| 1721 | 14 then |
| 1722 | 15 echo "Usage: `basename $0` USERNAME" |
| 1723 | 16 exit $E_WRONGARGS |
| 1724 | 17 fi |
| 1725 | 18 |
| 1726 | 19 file_excerpt () # Scan file for pattern, |
| 1727 | 20 { #+ then print relevant portion of line. |
| 1728 | 21 while read line # "while" does not necessarily need [ condition ] |
| 1729 | 22 do |
| 1730 | 23 echo "$line" | grep $1 | awk -F":" '{ print $5 }' |
| 1731 | 24 # Have awk use ":" delimiter. |
| 1732 | 25 done |
| 1733 | 26 } <$file # Redirect into function's stdin. |
| 1734 | 27 |
| 1735 | 28 file_excerpt $pattern |
| 1736 | 29 |
| 1737 | 30 # Yes, this entire script could be reduced to |
| 1738 | 31 # grep PATTERN /etc/passwd | awk -F":" '{ print $5 }' |
| 1739 | 32 # or |
| 1740 | 33 # awk -F: '/PATTERN/ {print $5}' |
| 1741 | 34 # or |
| 1742 | 35 # awk -F: '($1 == "username") { print $5 }' # real name from username |
| 1743 | 36 # However, it might not be as instructive. |
| 1744 | 37 |
| 1745 | 38 exit 0</PRE |
| 1746 | ></TD |
| 1747 | ></TR |
| 1748 | ></TABLE |
| 1749 | ><HR></DIV |
| 1750 | ><P |
| 1751 | >There is an alternate, and perhaps less confusing |
| 1752 | method of redirecting a function's |
| 1753 | <TT |
| 1754 | CLASS="FILENAME" |
| 1755 | >stdin</TT |
| 1756 | >. This involves redirecting the |
| 1757 | <TT |
| 1758 | CLASS="FILENAME" |
| 1759 | >stdin</TT |
| 1760 | > to an embedded bracketed code |
| 1761 | block within the function. |
| 1762 | |
| 1763 | <TABLE |
| 1764 | BORDER="0" |
| 1765 | BGCOLOR="#E0E0E0" |
| 1766 | WIDTH="90%" |
| 1767 | ><TR |
| 1768 | ><TD |
| 1769 | ><PRE |
| 1770 | CLASS="PROGRAMLISTING" |
| 1771 | > 1 # Instead of: |
| 1772 | 2 Function () |
| 1773 | 3 { |
| 1774 | 4 ... |
| 1775 | 5 } < file |
| 1776 | 6 |
| 1777 | 7 # Try this: |
| 1778 | 8 Function () |
| 1779 | 9 { |
| 1780 | 10 { |
| 1781 | 11 ... |
| 1782 | 12 } < file |
| 1783 | 13 } |
| 1784 | 14 |
| 1785 | 15 # Similarly, |
| 1786 | 16 |
| 1787 | 17 Function () # This works. |
| 1788 | 18 { |
| 1789 | 19 { |
| 1790 | 20 echo $* |
| 1791 | 21 } | tr a b |
| 1792 | 22 } |
| 1793 | 23 |
| 1794 | 24 Function () # This doesn't work. |
| 1795 | 25 { |
| 1796 | 26 echo $* |
| 1797 | 27 } | tr a b # A nested code block is mandatory here. |
| 1798 | 28 |
| 1799 | 29 |
| 1800 | 30 # Thanks, S.C.</PRE |
| 1801 | ></TD |
| 1802 | ></TR |
| 1803 | ></TABLE |
| 1804 | > |
| 1805 | </P |
| 1806 | ><DIV |
| 1807 | CLASS="NOTE" |
| 1808 | ><TABLE |
| 1809 | CLASS="NOTE" |
| 1810 | WIDTH="90%" |
| 1811 | BORDER="0" |
| 1812 | ><TR |
| 1813 | ><TD |
| 1814 | WIDTH="25" |
| 1815 | ALIGN="CENTER" |
| 1816 | VALIGN="TOP" |
| 1817 | ><IMG |
| 1818 | SRC="common/note.png" |
| 1819 | HSPACE="5" |
| 1820 | ALT="Note"></TD |
| 1821 | ><TD |
| 1822 | ALIGN="LEFT" |
| 1823 | VALIGN="TOP" |
| 1824 | ><P |
| 1825 | >Emmanuel Rouat's <A |
| 1826 | HREF="sample-bashrc.html" |
| 1827 | >sample <TT |
| 1828 | CLASS="FILENAME" |
| 1829 | >bashrc</TT |
| 1830 | > |
| 1831 | file</A |
| 1832 | > contains some instructive examples of |
| 1833 | functions.</P |
| 1834 | ></TD |
| 1835 | ></TR |
| 1836 | ></TABLE |
| 1837 | ></DIV |
| 1838 | ></DD |
| 1839 | ></DL |
| 1840 | ></DIV |
| 1841 | ></DIV |
| 1842 | ></DIV |
| 1843 | ><H3 |
| 1844 | CLASS="FOOTNOTES" |
| 1845 | >Notes</H3 |
| 1846 | ><TABLE |
| 1847 | BORDER="0" |
| 1848 | CLASS="FOOTNOTES" |
| 1849 | WIDTH="100%" |
| 1850 | ><TR |
| 1851 | ><TD |
| 1852 | ALIGN="LEFT" |
| 1853 | VALIGN="TOP" |
| 1854 | WIDTH="5%" |
| 1855 | ><A |
| 1856 | NAME="FTN.AEN18474" |
| 1857 | HREF="functions.html#AEN18474" |
| 1858 | >[1]</A |
| 1859 | ></TD |
| 1860 | ><TD |
| 1861 | ALIGN="LEFT" |
| 1862 | VALIGN="TOP" |
| 1863 | WIDTH="95%" |
| 1864 | ><P |
| 1865 | >The <B |
| 1866 | CLASS="COMMAND" |
| 1867 | >return</B |
| 1868 | > command is a |
| 1869 | Bash <A |
| 1870 | HREF="internal.html#BUILTINREF" |
| 1871 | >builtin</A |
| 1872 | >.</P |
| 1873 | ></TD |
| 1874 | ></TR |
| 1875 | ></TABLE |
| 1876 | ><DIV |
| 1877 | CLASS="NAVFOOTER" |
| 1878 | ><HR |
| 1879 | ALIGN="LEFT" |
| 1880 | WIDTH="100%"><TABLE |
| 1881 | SUMMARY="Footer navigation table" |
| 1882 | WIDTH="100%" |
| 1883 | BORDER="0" |
| 1884 | CELLPADDING="0" |
| 1885 | CELLSPACING="0" |
| 1886 | ><TR |
| 1887 | ><TD |
| 1888 | WIDTH="33%" |
| 1889 | ALIGN="left" |
| 1890 | VALIGN="top" |
| 1891 | ><A |
| 1892 | HREF="process-sub.html" |
| 1893 | ACCESSKEY="P" |
| 1894 | >Prev</A |
| 1895 | ></TD |
| 1896 | ><TD |
| 1897 | WIDTH="34%" |
| 1898 | ALIGN="center" |
| 1899 | VALIGN="top" |
| 1900 | ><A |
| 1901 | HREF="index.html" |
| 1902 | ACCESSKEY="H" |
| 1903 | >Home</A |
| 1904 | ></TD |
| 1905 | ><TD |
| 1906 | WIDTH="33%" |
| 1907 | ALIGN="right" |
| 1908 | VALIGN="top" |
| 1909 | ><A |
| 1910 | HREF="localvar.html" |
| 1911 | ACCESSKEY="N" |
| 1912 | >Next</A |
| 1913 | ></TD |
| 1914 | ></TR |
| 1915 | ><TR |
| 1916 | ><TD |
| 1917 | WIDTH="33%" |
| 1918 | ALIGN="left" |
| 1919 | VALIGN="top" |
| 1920 | >Process Substitution</TD |
| 1921 | ><TD |
| 1922 | WIDTH="34%" |
| 1923 | ALIGN="center" |
| 1924 | VALIGN="top" |
| 1925 | ><A |
| 1926 | HREF="part5.html" |
| 1927 | ACCESSKEY="U" |
| 1928 | >Up</A |
| 1929 | ></TD |
| 1930 | ><TD |
| 1931 | WIDTH="33%" |
| 1932 | ALIGN="right" |
| 1933 | VALIGN="top" |
| 1934 | >Local Variables</TD |
| 1935 | ></TR |
| 1936 | ></TABLE |
| 1937 | ></DIV |
| 1938 | ></BODY |
| 1939 | ></HTML |
| 1940 | > |