⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ivr.html

📁 Shall高级编程
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Indirect References</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Variables Revisited"HREF="variables2.html"><LINKREL="PREVIOUS"TITLE="Typing variables: declare or	  typeset"HREF="declareref.html"><LINKREL="NEXT"TITLE="$RANDOM: generate random integer"HREF="randomvar.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="declareref.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 9. Variables Revisited</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="randomvar.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="IVR"></A>9.5. Indirect References</H1><P><ANAME="IVRREF"></A></P><P>Assume that the value of a variable is the name of a second	  variable.  Is it somehow possible to retrieve the value	  of this second variable from the first one? For example,	  if <TTCLASS="REPLACEABLE"><I>a=letter_of_alphabet</I></TT>	  and <TTCLASS="REPLACEABLE"><I>letter_of_alphabet=z</I></TT>,	  can a reference to <TTCLASS="REPLACEABLE"><I>a</I></TT> return	  <TTCLASS="REPLACEABLE"><I>z</I></TT>? This can indeed be done, and	  it is called an <ICLASS="FIRSTTERM">indirect reference</I>.	  It uses the unusual <TTCLASS="REPLACEABLE"><I>eval var1=\$$var2</I></TT>	  notation.</P><DIVCLASS="EXAMPLE"><HR><ANAME="INDREF"></A><P><B>Example 9-24. Indirect Variable References</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# ind-ref.sh: Indirect variable referencing.   3&nbsp;# Accessing the contents of the contents of a variable.   4&nbsp;   5&nbsp;a=letter_of_alphabet   # Variable "a" holds the name of another variable.   6&nbsp;letter_of_alphabet=z   7&nbsp;   8&nbsp;echo   9&nbsp;  10&nbsp;# Direct reference.  11&nbsp;echo "a = $a"          # a = letter_of_alphabet  12&nbsp;  13&nbsp;# Indirect reference.  14&nbsp;eval a=\$$a  15&nbsp;echo "Now a = $a"      # Now a = z  16&nbsp;  17&nbsp;echo  18&nbsp;  19&nbsp;  20&nbsp;# Now, let's try changing the second-order reference.  21&nbsp;  22&nbsp;t=table_cell_3  23&nbsp;table_cell_3=24  24&nbsp;echo "\"table_cell_3\" = $table_cell_3"            # "table_cell_3" = 24  25&nbsp;echo -n "dereferenced \"t\" = "; eval echo \$$t    # dereferenced "t" = 24  26&nbsp;# In this simple case, the following also works (why?).  27&nbsp;#         eval t=\$$t; echo "\"t\" = $t"  28&nbsp;  29&nbsp;echo  30&nbsp;  31&nbsp;t=table_cell_3  32&nbsp;NEW_VAL=387  33&nbsp;table_cell_3=$NEW_VAL  34&nbsp;echo "Changing value of \"table_cell_3\" to $NEW_VAL."  35&nbsp;echo "\"table_cell_3\" now $table_cell_3"  36&nbsp;echo -n "dereferenced \"t\" now "; eval echo \$$t  37&nbsp;# "eval" takes the two arguments "echo" and "\$$t" (set equal to $table_cell_3)  38&nbsp;  39&nbsp;echo  40&nbsp;  41&nbsp;# (Thanks, Stephane Chazelas, for clearing up the above behavior.)  42&nbsp;  43&nbsp;  44&nbsp;# Another method is the ${!t} notation, discussed in "Bash, version 2" section.  45&nbsp;# See also ex78.sh.  46&nbsp;  47&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="IRRREF"></A>Of what practical use is indirect	  referencing of variables? It gives Bash a little of the	  functionality of <AHREF="variables.html#POINTERREF">pointers</A>	  in <ICLASS="FIRSTTERM">C</I>, for instance, in <AHREF="bash2.html#RESISTOR">table lookup</A>.  And, it also has some	  other very interesting applications. . . .</P><P>	  Nils Radtke shows how to build <SPANCLASS="QUOTE">"dynamic"</SPAN>          variable names and evaluate their contents. This can be useful	  when <AHREF="internal.html#SOURCEREF">sourcing</A> configuration files.           	   <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;   4&nbsp;# ---------------------------------------------   5&nbsp;# This could be "sourced" from a separate file.   6&nbsp;isdnMyProviderRemoteNet=172.16.0.100   7&nbsp;isdnYourProviderRemoteNet=10.0.0.10   8&nbsp;isdnOnlineService="MyProvider"   9&nbsp;# ---------------------------------------------  10&nbsp;        11&nbsp;  12&nbsp;remoteNet=$(eval "echo \$$(echo isdn${isdnOnlineService}RemoteNet)")  13&nbsp;remoteNet=$(eval "echo \$$(echo isdnMyProviderRemoteNet)")  14&nbsp;remoteNet=$(eval "echo \$isdnMyProviderRemoteNet")  15&nbsp;remoteNet=$(eval "echo $isdnMyProviderRemoteNet")  16&nbsp;  17&nbsp;echo "$remoteNet"    # 172.16.0.100  18&nbsp;  19&nbsp;# ================================================================  20&nbsp;  21&nbsp;#  And, it gets even better.  22&nbsp;  23&nbsp;#  Consider the following snippet given a variable named getSparc,  24&nbsp;#+ but no such variable getIa64:  25&nbsp;  26&nbsp;chkMirrorArchs () {   27&nbsp;  arch="$1";  28&nbsp;  if [ "$(eval "echo \${$(echo get$(echo -ne $arch |  29&nbsp;       sed 's/^\(.\).*/\1/g' | tr 'a-z' 'A-Z'; echo $arch |  30&nbsp;       sed 's/^.\(.*\)/\1/g')):-false}")" = true ]  31&nbsp;  then  32&nbsp;     return 0;  33&nbsp;  else  34&nbsp;     return 1;  35&nbsp;  fi;  36&nbsp;}  37&nbsp;  38&nbsp;getSparc="true"  39&nbsp;unset getIa64  40&nbsp;chkMirrorArchs sparc  41&nbsp;echo $?        # 0  42&nbsp;               # True  43&nbsp;  44&nbsp;chkMirrorArchs Ia64  45&nbsp;echo $?        # 1  46&nbsp;               # False  47&nbsp;  48&nbsp;# Notes:  49&nbsp;# -----  50&nbsp;# Even the to-be-substituted variable name part is built explicitly.  51&nbsp;# The parameters to the chkMirrorArchs calls are all lower case.  52&nbsp;# The variable name is composed of two parts: "get" and "Sparc" . . .</PRE></TD></TR></TABLE>        </P><DIVCLASS="EXAMPLE"><HR><ANAME="COLTOTALER2"></A><P><B>Example 9-25. Passing an indirect reference to <ICLASS="FIRSTTERM">awk</I></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;#  Another version of the "column totaler" script   4&nbsp;#+ that adds up a specified column (of numbers) in the target file.   5&nbsp;#  This one uses indirect references.   6&nbsp;   7&nbsp;ARGS=2   8&nbsp;E_WRONGARGS=65   9&nbsp;  10&nbsp;if [ $# -ne "$ARGS" ] # Check for proper no. of command line args.  11&nbsp;then  12&nbsp;   echo "Usage: `basename $0` filename column-number"  13&nbsp;   exit $E_WRONGARGS  14&nbsp;fi  15&nbsp;  16&nbsp;filename=$1  17&nbsp;column_number=$2  18&nbsp;  19&nbsp;#===== Same as original script, up to this point =====#  20&nbsp;  21&nbsp;  22&nbsp;# A multi-line awk script is invoked by   awk ' ..... '  23&nbsp;  24&nbsp;  25&nbsp;# Begin awk script.  26&nbsp;# ------------------------------------------------  27&nbsp;awk "  28&nbsp;  29&nbsp;{ total += \$${column_number} # indirect reference  30&nbsp;}  31&nbsp;END {  32&nbsp;     print total  33&nbsp;     }  34&nbsp;  35&nbsp;     " "$filename"  36&nbsp;# ------------------------------------------------  37&nbsp;# End awk script.  38&nbsp;  39&nbsp;#  Indirect variable reference avoids the hassles  40&nbsp;#+ of referencing a shell variable within the embedded awk script.  41&nbsp;#  Thanks, Stephane Chazelas.  42&nbsp;  43&nbsp;  44&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="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). <ANAME="IVR2"></A>Fortunately, the	<TTCLASS="REPLACEABLE"><I>${!variable}</I></TT> notation introduced	with <AHREF="bash2.html#BASH2REF">version 2</A> of Bash	(see <AHREF="bash2.html#EX78">Example 34-2</A> and <AHREF="contributed-scripts.html#HASHEX2">Example A-24</A>) makes	indirect referencing more intuitive.</P></TD></TR></TABLE></DIV><TABLECLASS="SIDEBAR"BORDER="1"CELLPADDING="5"><TR><TD><DIVCLASS="SIDEBAR"><ANAME="AEN5689"></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, an ugly kludge.</P></DIV></TD></TR></TABLE></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="declareref.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="randomvar.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Typing variables: <BCLASS="COMMAND">declare</B> or	  <BCLASS="COMMAND">typeset</B></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="variables2.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">$RANDOM: generate random integer</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -