1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Indirect References</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
"><LINK
REL="HOME"
TITLE="Advanced Bash-Scripting Guide"
HREF="index.html"><LINK
REL="UP"
TITLE="Advanced Topics"
HREF="part5.html"><LINK
REL="PREVIOUS"
TITLE="Arrays"
HREF="arrays.html"><LINK
REL="NEXT"
TITLE="/dev and /proc"
HREF="devproc.html"><META
HTTP-EQUIV="Content-Style-Type"
CONTENT="text/css"><LINK
REL="stylesheet"
HREF="common/kde-common.css"
TYPE="text/css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=iso-8859-1"><META
HTTP-EQUIV="Content-Language"
CONTENT="en"><LINK
REL="stylesheet"
HREF="common/kde-localised.css"
TYPE="text/css"
TITLE="KDE-English"><LINK
REL="stylesheet"
HREF="common/kde-default.css"
TYPE="text/css"
TITLE="KDE-Default"></HEAD
><BODY
CLASS="CHAPTER"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#AA0000"
VLINK="#AA0055"
ALINK="#AA0000"
STYLE="font-family: sans-serif;"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="arrays.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="devproc.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="IVR"
></A
>Chapter 28. Indirect References</H1
><P
><A
NAME="IVRREF"
></A
></P
><P
>We have seen that <A
HREF="variables.html#VARSUBN"
>referencing
a variable</A
>, <TT
CLASS="VARNAME"
>$var</TT
>, fetches its
<I
CLASS="FIRSTTERM"
>value</I
>. <A
NAME="EVALINDREF"
></A
>But,
what about the <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>value of a value</I
></SPAN
>? What
about <TT
CLASS="VARNAME"
>$$var</TT
>?</P
><P
>The actual notation is
<TT
CLASS="REPLACEABLE"
><I
>\$$var</I
></TT
>, usually preceded by
an <A
HREF="internal.html#EVALREF"
>eval</A
> (and sometimes an
<A
HREF="internal.html#ECHOREF"
>echo</A
>). This is called an
<I
CLASS="FIRSTTERM"
>indirect reference</I
>.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="INDREF"
></A
><P
><B
>Example 28-1. Indirect Variable References</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # ind-ref.sh: Indirect variable referencing.
3 # Accessing the contents of the contents of a variable.
4
5 # First, let's fool around a little.
6
7 var=23
8
9 echo "\$var = $var" # $var = 23
10 # So far, everything as expected. But ...
11
12 echo "\$\$var = $$var" # $$var = 4570var
13 # Not useful ...
14 # \$\$ expanded to PID of the script
15 # -- refer to the entry on the $$ variable --
16 #+ and "var" is echoed as plain text.
17 # (Thank you, Jakob Bohm, for pointing this out.)
18
19 echo "\\\$\$var = \$$var" # \$$var = $23
20 # As expected. The first $ is escaped and pasted on to
21 #+ the value of var ($var = 23 ).
22 # Meaningful, but still not useful.
23
24 # Now, let's start over and do it the right way.
25
26 # ============================================== #
27
28
29 a=letter_of_alphabet # Variable "a" holds the name of another variable.
30 letter_of_alphabet=z
31
32 echo
33
34 # Direct reference.
35 echo "a = $a" # a = letter_of_alphabet
36
37 # Indirect reference.
38 eval a=\$$a
39 # ^^^ Forcing an eval(uation), and ...
40 # ^ Escaping the first $ ...
41 # ------------------------------------------------------------------------
42 # The 'eval' forces an update of $a, sets it to the updated value of \$$a.
43 # So, we see why 'eval' so often shows up in indirect reference notation.
44 # ------------------------------------------------------------------------
45 echo "Now a = $a" # Now a = z
46
47 echo
48
49
50 # Now, let's try changing the second-order reference.
51
52 t=table_cell_3
53 table_cell_3=24
54 echo "\"table_cell_3\" = $table_cell_3" # "table_cell_3" = 24
55 echo -n "dereferenced \"t\" = "; eval echo \$$t # dereferenced "t" = 24
56 # In this simple case, the following also works (why?).
57 # eval t=\$$t; echo "\"t\" = $t"
58
59 echo
60
61 t=table_cell_3
62 NEW_VAL=387
63 table_cell_3=$NEW_VAL
64 echo "Changing value of \"table_cell_3\" to $NEW_VAL."
65 echo "\"table_cell_3\" now $table_cell_3"
66 echo -n "dereferenced \"t\" now "; eval echo \$$t
67 # "eval" takes the two arguments "echo" and "\$$t" (set equal to $table_cell_3)
68
69
70 echo
71
72 # (Thanks, Stephane Chazelas, for clearing up the above behavior.)
73
74
75 # A more straightforward method is the ${!t} notation, discussed in the
76 #+ "Bash, version 2" section.
77 # See also ex78.sh.
78
79 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
><A
NAME="IRRREF"
></A
></P
><TABLE
CLASS="SIDEBAR"
BORDER="1"
CELLPADDING="5"
><TR
><TD
><DIV
CLASS="SIDEBAR"
><A
NAME="AEN18998"
></A
><P
>Indirect referencing in Bash
is a multi-step process. First, take the name of a variable:
<TT
CLASS="VARNAME"
>varname</TT
>. Then, reference it:
<TT
CLASS="VARNAME"
>$varname</TT
>. Then, reference the reference:
<TT
CLASS="VARNAME"
>$$varname</TT
>. Then, <I
CLASS="FIRSTTERM"
>escape</I
>
the first <SPAN
CLASS="TOKEN"
>$</SPAN
>: <TT
CLASS="VARNAME"
>\$$varname</TT
>.
Finally, force a reevaluation of the expression and assign it:
<B
CLASS="COMMAND"
>eval newvar=\$$varname</B
>.</P
></DIV
></TD
></TR
></TABLE
><P
>Of what practical use is indirect referencing of
variables? It gives Bash a little of the functionality
of <A
HREF="variables.html#POINTERREF"
>pointers</A
>
in <I
CLASS="FIRSTTERM"
>C</I
>, for instance, in <A
HREF="bash2.html#RESISTOR"
>table lookup</A
>. And, it also has some
other very interesting applications. . . .</P
><P
>Nils Radtke shows how to build <SPAN
CLASS="QUOTE"
>"dynamic"</SPAN
>
variable names and evaluate their contents. This can be useful
when <A
HREF="internal.html#SOURCEREF"
>sourcing</A
> configuration
files.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3
4 # ---------------------------------------------
5 # This could be "sourced" from a separate file.
6 isdnMyProviderRemoteNet=172.16.0.100
7 isdnYourProviderRemoteNet=10.0.0.10
8 isdnOnlineService="MyProvider"
9 # ---------------------------------------------
10
11
12 remoteNet=$(eval "echo \$$(echo isdn${isdnOnlineService}RemoteNet)")
13 remoteNet=$(eval "echo \$$(echo isdnMyProviderRemoteNet)")
14 remoteNet=$(eval "echo \$isdnMyProviderRemoteNet")
15 remoteNet=$(eval "echo $isdnMyProviderRemoteNet")
16
17 echo "$remoteNet" # 172.16.0.100
18
19 # ================================================================
20
21 # And, it gets even better.
22
23 # Consider the following snippet given a variable named getSparc,
24 #+ but no such variable getIa64:
25
26 chkMirrorArchs () {
27 arch="$1";
28 if [ "$(eval "echo \${$(echo get$(echo -ne $arch |
29 sed 's/^\(.\).*/\1/g' | tr 'a-z' 'A-Z'; echo $arch |
30 sed 's/^.\(.*\)/\1/g')):-false}")" = true ]
31 then
32 return 0;
33 else
34 return 1;
35 fi;
36 }
37
38 getSparc="true"
39 unset getIa64
40 chkMirrorArchs sparc
41 echo $? # 0
42 # True
43
44 chkMirrorArchs Ia64
45 echo $? # 1
46 # False
47
48 # Notes:
49 # -----
50 # Even the to-be-substituted variable name part is built explicitly.
51 # The parameters to the chkMirrorArchs calls are all lower case.
52 # The variable name is composed of two parts: "get" and "Sparc" . . .</PRE
></TD
></TR
></TABLE
>
</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="COLTOTALER2"
></A
><P
><B
>Example 28-2. Passing an indirect reference to <I
CLASS="FIRSTTERM"
>awk</I
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3 # Another version of the "column totaler" script
4 #+ that adds up a specified column (of numbers) in the target file.
5 # This one uses indirect references.
6
7 ARGS=2
8 E_WRONGARGS=85
9
10 if [ $# -ne "$ARGS" ] # Check for proper number of command-line args.
11 then
12 echo "Usage: `basename $0` filename column-number"
13 exit $E_WRONGARGS
14 fi
15
16 filename=$1 # Name of file to operate on.
17 column_number=$2 # Which column to total up.
18
19 #===== Same as original script, up to this point =====#
20
21
22 # A multi-line awk script is invoked by
23 # awk "
24 # ...
25 # ...
26 # ...
27 # "
28
29
30 # Begin awk script.
31 # -------------------------------------------------
32 awk "
33
34 { total += \$${column_number} # Indirect reference
35 }
36 END {
37 print total
38 }
39
40 " "$filename"
41 # Note that awk doesn't need an eval preceding \$$.
42 # -------------------------------------------------
43 # End awk script.
44
45 # Indirect variable reference avoids the hassles
46 #+ of referencing a shell variable within the embedded awk script.
47 # Thanks, Stephane Chazelas.
48
49
50 exit $?</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="CAUTION"
><TABLE
CLASS="CAUTION"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/caution.png"
HSPACE="5"
ALT="Caution"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>This method of indirect referencing is a bit tricky.
If the second order variable changes its value, then the first
order variable must be properly dereferenced (as in the above
example). <A
NAME="IVR2"
></A
>Fortunately, the
<TT
CLASS="REPLACEABLE"
><I
>${!variable}</I
></TT
> notation introduced
with <A
HREF="bash2.html#BASH2REF"
>version 2</A
> of Bash
(see <A
HREF="bash2.html#EX78"
>Example 37-2</A
> and <A
HREF="contributed-scripts.html#HASHEX2"
>Example A-22</A
>) makes
indirect referencing more intuitive.</P
></TD
></TR
></TABLE
></DIV
><TABLE
CLASS="SIDEBAR"
BORDER="1"
CELLPADDING="5"
><TR
><TD
><DIV
CLASS="SIDEBAR"
><A
NAME="AEN19027"
></A
><P
>Bash does not support pointer arithmetic, and this severely
limits the usefulness of indirect referencing. In fact, indirect
referencing in a scripting language is, at best, something of
an afterthought.</P
></DIV
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="arrays.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="devproc.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Arrays</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="part5.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><TT
CLASS="FILENAME"
>/dev</TT
> and <TT
CLASS="FILENAME"
>/proc</TT
></TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|