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

📄 othertypesv.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 2 页
字号:
><ICLASS="FIRSTTERM">Bracket notation</I> for positional	      parameters leads to a fairly simple way of referencing	      the <SPANCLASS="emphasis"><ICLASS="EMPHASIS">last</I></SPAN> argument passed to a	      script on the command line. This also requires <AHREF="bash2.html#VARREFNEW">indirect referencing</A>.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;args=$#           # Number of args passed.   2&nbsp;lastarg=${!args}   3&nbsp;# Or:       lastarg=${!#}   4&nbsp;#           (Thanks, Chris Monson.)   5&nbsp;# Note that lastarg=${!$#} doesn't work.</PRE></TD></TR></TABLE></P><P>Some scripts can perform different operations,	      depending on which name they are invoked with. For this	      to work, the script needs to check <TTCLASS="VARNAME">$0</TT>,	      the name it was invoked by. There must also exist symbolic	      links to all the alternate names of the script. See <AHREF="external.html#HELLOL">Example 15-2</A>.</P><P><ANAME="NULLVAR"></A></P><DIVCLASS="TIP"><TABLECLASS="TIP"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/tip.png"HSPACE="5"ALT="Tip"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>If a script expects a command line parameter	      but is invoked without one, this may cause a <ICLASS="FIRSTTERM">null	      variable assignment</I>, generally an undesirable	      result. One way to prevent this is to append an extra	      character to both sides of the assignment statement using	      the expected positional parameter.  </P></TD></TR></TABLE></DIV><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;variable1_=$1_  # Rather than variable1=$1   2&nbsp;# This will prevent an error, even if positional parameter is absent.   3&nbsp;   4&nbsp;critical_argument01=$variable1_   5&nbsp;   6&nbsp;# The extra character can be stripped off later, like so.   7&nbsp;variable1=${variable1_/_/}   8&nbsp;# Side effects only if $variable1_ begins with an underscore.   9&nbsp;# This uses one of the parameter substitution templates discussed later.  10&nbsp;# (Leaving out the replacement pattern results in a deletion.)  11&nbsp;  12&nbsp;#  A more straightforward way of dealing with this is  13&nbsp;#+ to simply test whether expected positional parameters have been passed.  14&nbsp;if [ -z $1 ]  15&nbsp;then  16&nbsp;  exit $E_MISSING_POS_PARAM  17&nbsp;fi  18&nbsp;  19&nbsp;  20&nbsp;#  However, as Fabian Kreutz points out,  21&nbsp;#+ the above method may have unexpected side-effects.  22&nbsp;#  A better method is parameter substitution:  23&nbsp;#         ${1:-$DefaultVal}  24&nbsp;#  See the "Parameter Substition" section  25&nbsp;#+ in the "Variables Revisited" chapter.</PRE></TD></TR></TABLE><P>---</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX18"></A><P><B>Example 4-6. <ICLASS="FIRSTTERM">wh</I>, <ICLASS="FIRSTTERM">                whois</I> domain name lookup</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# ex18.sh   3&nbsp;   4&nbsp;# Does a 'whois domain-name' lookup on any of 3 alternate servers:   5&nbsp;#                    ripe.net, cw.net, radb.net   6&nbsp;   7&nbsp;# Place this script -- renamed 'wh' -- in /usr/local/bin   8&nbsp;   9&nbsp;# Requires symbolic links:  10&nbsp;# ln -s /usr/local/bin/wh /usr/local/bin/wh-ripe  11&nbsp;# ln -s /usr/local/bin/wh /usr/local/bin/wh-cw  12&nbsp;# ln -s /usr/local/bin/wh /usr/local/bin/wh-radb  13&nbsp;  14&nbsp;E_NOARGS=65  15&nbsp;  16&nbsp;  17&nbsp;if [ -z "$1" ]  18&nbsp;then  19&nbsp;  echo "Usage: `basename $0` [domain-name]"  20&nbsp;  exit $E_NOARGS  21&nbsp;fi  22&nbsp;  23&nbsp;# Check script name and call proper server.  24&nbsp;case `basename $0` in    # Or:    case ${0##*/} in  25&nbsp;    "wh"     ) whois $1@whois.ripe.net;;  26&nbsp;    "wh-ripe") whois $1@whois.ripe.net;;  27&nbsp;    "wh-radb") whois $1@whois.radb.net;;  28&nbsp;    "wh-cw"  ) whois $1@whois.cw.net;;  29&nbsp;    *        ) echo "Usage: `basename $0` [domain-name]";;  30&nbsp;esac   31&nbsp;  32&nbsp;exit $?</PRE></TD></TR></TABLE><HR></DIV><P>---</P><P><ANAME="SHIFTREF"></A></P><P>	      	      	      The <BCLASS="COMMAND">shift</B> command reassigns the positional	      parameters, in effect shifting them to the left one notch.</P><P><TTCLASS="VARNAME">$1</TT> &#60;--- <TTCLASS="VARNAME">$2</TT>, <TTCLASS="VARNAME">$2</TT> &#60;--- <TTCLASS="VARNAME">$3</TT>, <TTCLASS="VARNAME">$3</TT> &#60;--- <TTCLASS="VARNAME">$4</TT>, etc.</P><P>The old <TTCLASS="VARNAME">$1</TT> disappears, but	      <SPANCLASS="emphasis"><ICLASS="EMPHASIS"><TTCLASS="VARNAME">$0</TT> (the script name)	      does not change</I></SPAN>. If you use a large number of	      positional parameters to a script, <BCLASS="COMMAND">shift</B>	      lets you access those past <TTCLASS="LITERAL">10</TT>, although	      <AHREF="othertypesv.html#BRACKETNOTATION">{bracket} notation</A>	      also permits this.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX19"></A><P><B>Example 4-7. Using <ICLASS="FIRSTTERM">shift</I></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# shft.sh: Using 'shift' to step through all the positional parameters   3&nbsp;   4&nbsp;#  Name this script something like shft.sh,   5&nbsp;#+ and invoke it with some parameters.   6&nbsp;#+ For example:   7&nbsp;#             sh shft.sh a b c def 23 skidoo   8&nbsp;   9&nbsp;until [ -z "$1" ]  # Until all parameters used up . . .  10&nbsp;do  11&nbsp;  echo -n "$1 "  12&nbsp;  shift  13&nbsp;done  14&nbsp;  15&nbsp;echo               # Extra line feed.  16&nbsp;  17&nbsp;exit 0  18&nbsp;  19&nbsp;#  See also the echo-params.sh script for a "shiftless"  20&nbsp;#+ alternative method of stepping through the positional params.</PRE></TD></TR></TABLE><HR></DIV><P>The <BCLASS="COMMAND">shift</B> command can take a numerical	    parameter indicating how many positions to shift.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# shift-past.sh   3&nbsp;   4&nbsp;shift 3    # Shift 3 positions.   5&nbsp;#  n=3; shift $n   6&nbsp;#  Has the same effect.   7&nbsp;   8&nbsp;echo "$1"   9&nbsp;  10&nbsp;exit 0  11&nbsp;  12&nbsp;# ======================== #  13&nbsp;  14&nbsp;  15&nbsp;$ sh shift-past.sh 1 2 3 4 5  16&nbsp;4  17&nbsp;  18&nbsp;#  However, as Eleni Fragkiadaki, points out,  19&nbsp;#+ attempting a 'shift' past the number of  20&nbsp;#+ positional parameters ($#) returns an exit status of 0,  21&nbsp;#+ and the positional parameters themselves do not change.  22&nbsp;#  This means possibly getting stuck in an endless loop. . . .  23&nbsp;#  For example:  24&nbsp;#      until [ -z "$1" ]  25&nbsp;#      do  26&nbsp;#         echo -n "$1 "  27&nbsp;#         shift 20    #  If less than 20 pos params,  28&nbsp;#      done           #+ then loop never ends!  29&nbsp;#  30&nbsp;# When in doubt, add a sanity check. . . .  31&nbsp;#           shift 20 || break  32&nbsp;#                    ^^^^^^^^</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>The <BCLASS="COMMAND">shift</B> command works in a similar	    fashion on parameters passed to a <AHREF="functions.html#FUNCTIONREF">function</A>.  See <AHREF="assortedtips.html#MULTIPLICATION">Example 33-15</A>.</P></TD></TR></TABLE></DIV></DD></DL></DIV></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN2193"HREF="othertypesv.html#AEN2193">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>The process calling the script sets the	        <TTCLASS="VARNAME">$0</TT> parameter. By convention, this		parameter is the name of the script. See the <AHREF="external.html#MANREF">manpage</A> (manual page) for		<BCLASS="COMMAND">execv</B>.</P></TD></TR></TABLE><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="untyped.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="quoting.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Bash Variables Are Untyped</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="variables.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Quoting</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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