Debian Codegraph @

HTML/ivr.html

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