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

📄 parameter-substitution.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Parameter Substitution</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="Manipulating Strings"HREF="string-manipulation.html"><LINKREL="NEXT"TITLE="Typing variables: declare or	  typeset"HREF="declareref.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="string-manipulation.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 9. Variables Revisited</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="declareref.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="PARAMETER-SUBSTITUTION"></A>9.3. Parameter Substitution</H1><P><ANAME="PARAMSUBREF"></A></P><DIVCLASS="VARIABLELIST"><P><B><ANAME="PSSUB1"></A>Manipulating and/or expanding variables</B></P><DL><DT><TTCLASS="USERINPUT"><B>${parameter}</B></TT></DT><DD><P>Same as <TTCLASS="REPLACEABLE"><I>$parameter</I></TT>, i.e.,		  value of the variable		  <TTCLASS="REPLACEABLE"><I>parameter</I></TT>.		  In certain contexts, only the less ambiguous		  <TTCLASS="REPLACEABLE"><I>${parameter}</I></TT> form		  works.</P><P>May be used for concatenating variables with strings.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;your_id=${USER}-on-${HOSTNAME}   2&nbsp;echo "$your_id"   3&nbsp;#   4&nbsp;echo "Old \$PATH = $PATH"   5&nbsp;PATH=${PATH}:/opt/bin  #Add /opt/bin to $PATH for duration of script.   6&nbsp;echo "New \$PATH = $PATH"</PRE></TD></TR></TABLE></P></DD><DT><ANAME="DEFPARAM1"></A><TTCLASS="USERINPUT"><B>${parameter-default}</B></TT>, <TTCLASS="USERINPUT"><B>${parameter:-default}</B></TT></DT><DD><P>If parameter not set, use default.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;echo ${username-`whoami`}   2&nbsp;# Echoes the result of `whoami`, if variable $username is still unset.</PRE></TD></TR></TABLE></P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P><TTCLASS="REPLACEABLE"><I>${parameter-default}</I></TT>		  and <TTCLASS="REPLACEABLE"><I>${parameter:-default}</I></TT>		  are almost equivalent. The extra <SPANCLASS="TOKEN">:</SPAN> makes		  a difference only when <TTCLASS="PARAMETER"><I>parameter</I></TT>		  has been declared, but is null.  </P></TD></TR></TABLE></DIV><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# param-sub.sh   3&nbsp;   4&nbsp;#  Whether a variable has been declared   5&nbsp;#+ affects triggering of the default option   6&nbsp;#+ even if the variable is null.   7&nbsp;   8&nbsp;username0=   9&nbsp;echo "username0 has been declared, but is set to null."  10&nbsp;echo "username0 = ${username0-`whoami`}"  11&nbsp;# Will not echo.  12&nbsp;  13&nbsp;echo  14&nbsp;  15&nbsp;echo username1 has not been declared.  16&nbsp;echo "username1 = ${username1-`whoami`}"  17&nbsp;# Will echo.  18&nbsp;  19&nbsp;username2=  20&nbsp;echo "username2 has been declared, but is set to null."  21&nbsp;echo "username2 = ${username2:-`whoami`}"  22&nbsp;#                            ^  23&nbsp;# Will echo because of :- rather than just - in condition test.  24&nbsp;# Compare to first instance, above.  25&nbsp;  26&nbsp;  27&nbsp;#  28&nbsp;  29&nbsp;# Once again:  30&nbsp;  31&nbsp;variable=  32&nbsp;# variable has been declared, but is set to null.  33&nbsp;  34&nbsp;echo "${variable-0}"    # (no output)  35&nbsp;echo "${variable:-1}"   # 1  36&nbsp;#               ^  37&nbsp;  38&nbsp;unset variable  39&nbsp;  40&nbsp;echo "${variable-2}"    # 2  41&nbsp;echo "${variable:-3}"   # 3  42&nbsp;  43&nbsp;exit 0</PRE></TD></TR></TABLE></P><P>The <ICLASS="FIRSTTERM">default parameter</I> construct		  finds use in providing <SPANCLASS="QUOTE">"missing"</SPAN> command-line		  arguments in scripts.</P><P>		  <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;DEFAULT_FILENAME=generic.data   2&nbsp;filename=${1:-$DEFAULT_FILENAME}   3&nbsp;#  If not otherwise specified, the following command block operates   4&nbsp;#+ on the file "generic.data".   5&nbsp;#   6&nbsp;#  Commands follow.</PRE></TD></TR></TABLE>		</P><P>See also <AHREF="special-chars.html#EX58">Example 3-4</A>, <AHREF="zeros.html#EX73">Example 28-2</A>, and <AHREF="contributed-scripts.html#COLLATZ">Example A-6</A>.</P><P>Compare this method with <AHREF="list-cons.html#ANDDEFAULT">using an <ICLASS="FIRSTTERM">and		  list</I> to supply a default command-line		  argument</A>.</P></DD><DT><TTCLASS="USERINPUT"><B>${parameter=default}</B></TT>, <TTCLASS="USERINPUT"><B>${parameter:=default}</B></TT></DT><DD><P><ANAME="DEFPARAM"></A></P><P>If parameter not set, set it to		  <ICLASS="FIRSTTERM">default</I>.</P><P>Both forms nearly equivalent. The <SPANCLASS="TOKEN">:</SPAN>		  makes a difference only when <TTCLASS="VARNAME">$parameter</TT>		  has been declared and is null,		    <ANAME="AEN5317"HREF="#FTN.AEN5317">[1]</A>		  as above.		    </P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;echo ${username=`whoami`}   2&nbsp;# Variable "username" is now set to `whoami`.</PRE></TD></TR></TABLE></P></DD><DT><ANAME="PARAMALTV"></A><TTCLASS="USERINPUT"><B>${parameter+alt_value}</B></TT>, <TTCLASS="USERINPUT"><B>${parameter:+alt_value}</B></TT></DT><DD><P>If parameter set, use		    <TTCLASS="USERINPUT"><B>alt_value</B></TT>, else use null		    string.</P><P>Both forms nearly equivalent. The <SPANCLASS="TOKEN">:</SPAN>		    makes a difference only when		    <TTCLASS="PARAMETER"><I>parameter</I></TT>		    has been declared and is null, see below.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;echo "###### \${parameter+alt_value} ########"   2&nbsp;echo   3&nbsp;   4&nbsp;a=${param1+xyz}   5&nbsp;echo "a = $a"      # a =   6&nbsp;   7&nbsp;param2=   8&nbsp;a=${param2+xyz}   9&nbsp;echo "a = $a"      # a = xyz  10&nbsp;  11&nbsp;param3=123  12&nbsp;a=${param3+xyz}  13&nbsp;echo "a = $a"      # a = xyz  14&nbsp;  15&nbsp;echo  16&nbsp;echo "###### \${parameter:+alt_value} ########"  17&nbsp;echo  18&nbsp;  19&nbsp;a=${param4:+xyz}  20&nbsp;echo "a = $a"      # a =  21&nbsp;  22&nbsp;param5=  23&nbsp;a=${param5:+xyz}  24&nbsp;echo "a = $a"      # a =  25&nbsp;# Different result from   a=${param5+xyz}  26&nbsp;  27&nbsp;param6=123  28&nbsp;a=${param6:+xyz}  29&nbsp;echo "a = $a"      # a = xyz</PRE></TD></TR></TABLE></P></DD><DT><ANAME="QERRMSG"></A><TTCLASS="USERINPUT"><B>${parameter?err_msg}</B></TT>, <TTCLASS="USERINPUT"><B>${parameter:?err_msg}</B></TT></DT><DD><P>If parameter set, use it, else print err_msg.</P><P>Both forms nearly equivalent. The <SPANCLASS="TOKEN">:</SPAN>		  makes a difference only when <TTCLASS="PARAMETER"><I>parameter</I></TT>		  has been declared and is null, as above.</P></DD></DL></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="EX6"></A><P><B>Example 9-16. Using parameter substitution and error messages</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;#  Check some of the system's environmental variables.   4&nbsp;#  This is good preventative maintenance.   5&nbsp;#  If, for example, $USER, the name of the person at the console, is not set,   6&nbsp;#+ the machine will not recognize you.   7&nbsp;   8&nbsp;: ${HOSTNAME?} ${USER?} ${HOME?} ${MAIL?}   9&nbsp;  echo  10&nbsp;  echo "Name of the machine is $HOSTNAME."  11&nbsp;  echo "You are $USER."  12&nbsp;  echo "Your home directory is $HOME."  13&nbsp;  echo "Your mail INBOX is located in $MAIL."  14&nbsp;  echo  15&nbsp;  echo "If you are reading this message,"  16&nbsp;  echo "critical environmental variables have been set."  17&nbsp;  echo  18&nbsp;  echo  19&nbsp;  20&nbsp;# ------------------------------------------------------  21&nbsp;  22&nbsp;#  The ${variablename?} construction can also check  23&nbsp;#+ for variables set within the script.  24&nbsp;  25&nbsp;ThisVariable=Value-of-ThisVariable  26&nbsp;#  Note, by the way, that string variables may be set  27&nbsp;#+ to characters disallowed in their names.  28&nbsp;: ${ThisVariable?}  29&nbsp;echo "Value of ThisVariable is $ThisVariable".  30&nbsp;echo  31&nbsp;echo  32&nbsp;  33&nbsp;  34&nbsp;: ${ZZXy23AB?"ZZXy23AB has not been set."}  35&nbsp;#  If ZZXy23AB has not been set,  36&nbsp;#+ then the script terminates with an error message.  37&nbsp;  38&nbsp;# You can specify the error message.  39&nbsp;# : ${variablename?"ERROR MESSAGE"}  40&nbsp;  41&nbsp;  42&nbsp;# Same result with:    dummy_variable=${ZZXy23AB?}  43&nbsp;#                      dummy_variable=${ZZXy23AB?"ZXy23AB has not been set."}  44&nbsp;#  45&nbsp;#                      echo ${ZZXy23AB?} &#62;/dev/null  46&nbsp;

⌨️ 快捷键说明

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