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

📄 variables2.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
📖 第 1 页 / 共 4 页
字号:
><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P><SPANCLASS="QUOTE">"<TTCLASS="VARNAME">$*</TT>"</SPAN> must be	    quoted.</P></TD></TR></TABLE></DIV></DD><DT><TTCLASS="VARNAME">$@</TT></DT><DD><P>Same as <SPANCLASS="TOKEN">$*</SPAN>, but each parameter is a	      quoted string, that is, the parameters are passed on	      intact, without interpretation or expansion. This means,	      among other things, that each parameter in the argument	      list is seen as a separate word.</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>Of course, <SPANCLASS="QUOTE">"<TTCLASS="VARNAME">$@</TT>"</SPAN>	    should be quoted.</P></TD></TR></TABLE></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="ARGLIST"></A><P><B>Example 9-6. <BCLASS="COMMAND">arglist</B>: Listing arguments with $* and $@</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# arglist.sh   3&nbsp;# Invoke this script with several arguments, such as "one two three".   4&nbsp;   5&nbsp;E_BADARGS=65   6&nbsp;   7&nbsp;if [ ! -n "$1" ]   8&nbsp;then   9&nbsp;  echo "Usage: `basename $0` argument1 argument2 etc."  10&nbsp;  exit $E_BADARGS  11&nbsp;fi    12&nbsp;  13&nbsp;echo  14&nbsp;  15&nbsp;index=1          # Initialize count.  16&nbsp;  17&nbsp;echo "Listing args with \"\$*\":"  18&nbsp;for arg in "$*"  # Doesn't work properly if "$*" isn't quoted.  19&nbsp;do  20&nbsp;  echo "Arg #$index = $arg"  21&nbsp;  let "index+=1"  22&nbsp;done             # $* sees all arguments as single word.   23&nbsp;echo "Entire arg list seen as single word."  24&nbsp;  25&nbsp;echo  26&nbsp;  27&nbsp;index=1          # Reset count.  28&nbsp;                 # What happens if you forget to do this?  29&nbsp;  30&nbsp;echo "Listing args with \"\$@\":"  31&nbsp;for arg in "$@"  32&nbsp;do  33&nbsp;  echo "Arg #$index = $arg"  34&nbsp;  let "index+=1"  35&nbsp;done             # $@ sees arguments as separate words.   36&nbsp;echo "Arg list seen as separate words."  37&nbsp;  38&nbsp;echo  39&nbsp;  40&nbsp;index=1          # Reset count.  41&nbsp;  42&nbsp;echo "Listing args with \$* (unquoted):"  43&nbsp;for arg in $*  44&nbsp;do  45&nbsp;  echo "Arg #$index = $arg"  46&nbsp;  let "index+=1"  47&nbsp;done             # Unquoted $* sees arguments as separate words.   48&nbsp;echo "Arg list seen as separate words."  49&nbsp;  50&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>Following a <BCLASS="COMMAND">shift</B>, the	      <TTCLASS="VARNAME">$@</TT> holds the remaining command-line	      parameters, lacking the previous <TTCLASS="VARNAME">$1</TT>,	      which was lost.	        <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# Invoke with ./scriptname 1 2 3 4 5   3&nbsp;   4&nbsp;echo "$@"    # 1 2 3 4 5   5&nbsp;shift   6&nbsp;echo "$@"    # 2 3 4 5   7&nbsp;shift   8&nbsp;echo "$@"    # 3 4 5   9&nbsp;  10&nbsp;# Each "shift" loses parameter $1.  11&nbsp;# "$@" then contains the remaining parameters.</PRE></TD></TR></TABLE>            </P><P>The <TTCLASS="VARNAME">$@</TT> special parameter finds	      use as a tool for filtering input into shell scripts. The	      <BCLASS="COMMAND">cat "$@"</B> construction accepts input	      to a script either from <TTCLASS="FILENAME">stdin</TT> or	      from files given as parameters to the script. See <AHREF="textproc.html#ROT13">Example 12-21</A> and <AHREF="textproc.html#CRYPTOQUOTE">Example 12-22</A>.</P><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The <TTCLASS="VARNAME">$*</TT> and <TTCLASS="VARNAME">$@</TT>	      parameters sometimes display inconsistent and	      puzzling behavior, depending on the setting of <AHREF="variables2.html#IFSREF">$IFS</A>.</P></TD></TR></TABLE></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="INCOMPAT"></A><P><B>Example 9-7. Inconsistent <TTCLASS="VARNAME">$*</TT> and <TTCLASS="VARNAME">$@</TT> behavior</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;#  Erratic behavior of the "$*" and "$@" internal Bash variables,   4&nbsp;#+ depending on whether they are quoted or not.   5&nbsp;#  Inconsistent handling of word splitting and linefeeds.   6&nbsp;   7&nbsp;   8&nbsp;set -- "First one" "second" "third:one" "" "Fifth: :one"   9&nbsp;# Setting the script arguments, $1, $2, etc.  10&nbsp;  11&nbsp;echo  12&nbsp;  13&nbsp;echo 'IFS unchanged, using "$*"'  14&nbsp;c=0  15&nbsp;for i in "$*"               # quoted  16&nbsp;do echo "$((c+=1)): [$i]"   # This line remains the same in every instance.  17&nbsp;                            # Echo args.  18&nbsp;done  19&nbsp;echo ---  20&nbsp;  21&nbsp;echo 'IFS unchanged, using $*'  22&nbsp;c=0  23&nbsp;for i in $*                 # unquoted  24&nbsp;do echo "$((c+=1)): [$i]"  25&nbsp;done  26&nbsp;echo ---  27&nbsp;  28&nbsp;echo 'IFS unchanged, using "$@"'  29&nbsp;c=0  30&nbsp;for i in "$@"  31&nbsp;do echo "$((c+=1)): [$i]"  32&nbsp;done  33&nbsp;echo ---  34&nbsp;  35&nbsp;echo 'IFS unchanged, using $@'  36&nbsp;c=0  37&nbsp;for i in $@  38&nbsp;do echo "$((c+=1)): [$i]"  39&nbsp;done  40&nbsp;echo ---  41&nbsp;  42&nbsp;IFS=:  43&nbsp;echo 'IFS=":", using "$*"'  44&nbsp;c=0  45&nbsp;for i in "$*"  46&nbsp;do echo "$((c+=1)): [$i]"  47&nbsp;done  48&nbsp;echo ---  49&nbsp;  50&nbsp;echo 'IFS=":", using $*'  51&nbsp;c=0  52&nbsp;for i in $*  53&nbsp;do echo "$((c+=1)): [$i]"  54&nbsp;done  55&nbsp;echo ---  56&nbsp;  57&nbsp;var=$*  58&nbsp;echo 'IFS=":", using "$var" (var=$*)'  59&nbsp;c=0  60&nbsp;for i in "$var"  61&nbsp;do echo "$((c+=1)): [$i]"  62&nbsp;done  63&nbsp;echo ---  64&nbsp;  65&nbsp;echo 'IFS=":", using $var (var=$*)'  66&nbsp;c=0  67&nbsp;for i in $var  68&nbsp;do echo "$((c+=1)): [$i]"  69&nbsp;done  70&nbsp;echo ---  71&nbsp;  72&nbsp;var="$*"  73&nbsp;echo 'IFS=":", using $var (var="$*")'  74&nbsp;c=0  75&nbsp;for i in $var  76&nbsp;do echo "$((c+=1)): [$i]"  77&nbsp;done  78&nbsp;echo ---  79&nbsp;  80&nbsp;echo 'IFS=":", using "$var" (var="$*")'  81&nbsp;c=0  82&nbsp;for i in "$var"  83&nbsp;do echo "$((c+=1)): [$i]"  84&nbsp;done  85&nbsp;echo ---  86&nbsp;  87&nbsp;echo 'IFS=":", using "$@"'  88&nbsp;c=0  89&nbsp;for i in "$@"  90&nbsp;do echo "$((c+=1)): [$i]"  91&nbsp;done  92&nbsp;echo ---  93&nbsp;  94&nbsp;echo 'IFS=":", using $@'  95&nbsp;c=0  96&nbsp;for i in $@  97&nbsp;do echo "$((c+=1)): [$i]"  98&nbsp;done  99&nbsp;echo --- 100&nbsp; 101&nbsp;var=$@ 102&nbsp;echo 'IFS=":", using $var (var=$@)' 103&nbsp;c=0 104&nbsp;for i in $var 105&nbsp;do echo "$((c+=1)): [$i]" 106&nbsp;done 107&nbsp;echo --- 108&nbsp; 109&nbsp;echo 'IFS=":", using "$var" (var=$@)' 110&nbsp;c=0 111&nbsp;for i in "$var" 112&nbsp;do echo "$((c+=1)): [$i]" 113&nbsp;done 114&nbsp;echo --- 115&nbsp; 116&nbsp;var="$@" 117&nbsp;echo 'IFS=":", using "$var" (var="$@")' 118&nbsp;c=0 119&nbsp;for i in "$var" 120&nbsp;do echo "$((c+=1)): [$i]" 121&nbsp;done 122&nbsp;echo --- 123&nbsp; 124&nbsp;echo 'IFS=":", using $var (var="$@")' 125&nbsp;c=0 126&nbsp;for i in $var 127&nbsp;do echo "$((c+=1)): [$i]" 128&nbsp;done 129&nbsp; 130&nbsp;echo 131&nbsp; 132&nbsp;# Try this script with ksh or zsh -y. 133&nbsp; 134&nbsp;exit 0 135&nbsp; 136&nbsp;# This example script by Stephane Chazelas, 137&nbsp;# and slightly modified by the document author.</PRE></TD></TR></TABLE><HR></DIV><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">$@</B> and <BCLASS="COMMAND">$*</B>	      parameters differ only when between double quotes.</P></TD></TR></TABLE></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="IFSEMPTY"></A><P><B>Example 9-8. <TTCLASS="VARNAME">$*</TT> and <TTCLASS="VARNAME">$@</TT> when	        <TTCLASS="VARNAME">$IFS</TT> is empty</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;#  If $IFS set, but empty,   4&nbsp;#+ then "$*" and "$@" do not echo positional params as expected.   5&nbsp;   6&nbsp;mecho ()       # Echo positional parameters.   7&nbsp;{   8&nbsp;echo "$1,$2,$3";   9&nbsp;}  10&nbsp;  11&nbsp;  12&nbsp;IFS=""         # Set, but empty.  13&nbsp;set a b c      # Positional parameters.  14&nbsp;  15&nbsp;mecho "$*"     # abc,,  16&nbsp;mecho $*       # a,b,c  17&nbsp;  18&nbsp;mecho $@       # a,b,c  19&nbsp;mecho "$@"     # a,b,c  20&nbsp;  21&nbsp;#  The behavior of $* and $@ when $IFS is empty depends  22&nbsp;#+ on whatever Bash or sh version being run.  23&nbsp;#  It is therefore inadvisable to depend on this "feature" in a script.  24&nbsp;  25&nbsp;  26&nbsp;# Thanks, Stephane Chazelas.  27&nbsp;  28&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B>Other Special Parameters</B></P><DL><DT><ANAME="FLPREF"></A><TTCLASS="VARNAME">$-</TT></DT><DD><P>Flags passed to script (using <AHREF="internal.html#SETREF">set</A>). See <AHREF="internal.html#EX34">Example 11-15</A>.</P><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>This was originally a <ICLASS="EMPHASIS">ksh</I>	      construct adopted into Bash, and unfortunately it does not	      seem to work reliably in Bash scripts. One possible use	      for it is to have a script <AHREF="miscellany.html#IITEST">self-test	      whether it is interactive</A>.</P></TD></TR></TABLE></DIV></DD><DT><TTCLASS="VARNAME">$!</TT></DT><DD><P>PID (process ID) of last job run in background</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;LOG=$0.log   2&nbsp;   3&nbsp;COMMAND1="sleep 100"   4&nbsp;   5&nbsp;echo "Logging PIDs background commands for script: $0" &#62;&#62; "$LOG"   6&nbsp;# So they can be monitored, and killed as necessary.   7&nbsp;echo &#62;&#62; "$LOG"   8&nbsp;   9&nbsp;# Logging commands.  10&nbsp;  11&nbsp;echo -n "PID of \"$COMMAND1\":  " &#62;&#62; "$LOG"  12&nbsp;${COMMAND1} &#38;  13&nbsp;echo $! &#62;&#62; "$LOG"  14&nbsp;# PID of "sleep 100":  1506  15&nbsp;  16&nbsp;# Thank you, Jacques Lederer, for suggesting this.</PRE></TD></TR></TABLE>            </P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;possibly_hanging_job &#38; { sleep ${TIMEOUT}; eval 'kill -9 $!' &#38;&#62; /dev/null; }   2&nbsp;# Forces completion of an ill-behaved program.   3&nbsp;# Useful, for example, in init scripts.   4&nbsp;   5&nbsp;# Thank you, Sylvain Fourmanoit, for this creative use of the "!" variable.</PRE></TD></TR></TABLE>            </P></DD><DT><ANAME="UNDERSCOREREF"></A><TTCLASS="VARNAME">$_</TT></DT><DD><P>Special variable set to last argument of previous command	        executed.</P><DIVCLASS="EXAMPLE"><HR><ANAME="USCREF"></A><P><B>Example 9-9. Underscore variable</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;echo $_              # /bin/bash   4&nbsp;                     # Just called /bin/bash to run the script.   5&nbsp;   6&nbsp;du &#62;/dev/null        # So no output from command.   7&nbsp;echo $_              # du   8&nbsp;   9&nbsp;ls -al &#62;/dev/null    # So no output from command.  10&nbsp;echo $_              # -al  (last argument)  11&nbsp;  12&nbsp;:  13&nbsp;echo $_              # :</PRE></TD></TR></TABLE><HR></DIV></DD><DT><ANAME="XSTATVARREF"></A><TTCLASS="VARNAME">$?</TT></DT><DD><P><AHREF="exit-status.html#EXITSTATUSREF">Exit status</A>	    of a command, <AHREF="functions.html#FUNCTIONREF">function</A>,	    or the script itself (see <AHREF="functions.html#MAX">Example 23-7</A>)</P></DD><DT><ANAME="PROCCID"></A><TTCLASS="VARNAME">$$</TT></DT><DD><P>Process ID of the script itself. The	  <TTCLASS="VARNAME">$$</TT> variable often finds use	    in scripts to construct <SPANCLASS="QUOTE">"unique"</SPAN>	    temp file names (see <AHREF="contributed-scripts.html#FTPGET">Example A-13</A>, <AHREF="debugging.html#ONLINE">Example 29-6</A>, <AHREF="filearchiv.html#DERPM">Example 12-28</A>, and <AHREF="internal.html#SELFDESTRUCT">Example 11-25</A>). This is usually simpler than	    invoking <AHREF="filearchiv.html#MKTEMPREF">mktemp</A>.</P></DD></DL></DIV></DIV></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN4002"HREF="variables2.html#AEN4002">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>The PID of the currently running script is	      <TTCLASS="VARNAME">$$</TT>, of course.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN4301"HREF="variables2.html#AEN4301">[2]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>The words <SPANCLASS="QUOTE">"argument"</SPAN>		and <SPANCLASS="QUOTE">"parameter"</SPAN> are often used		interchangeably. In the context of this document, they		have the same precise meaning, that of a variable passed		to a script or function.</P></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="part3.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="string-manipulation.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Beyond the Basics</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part3.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Manipulating Strings</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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