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

📄 internal.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
 BASH_VERSION=$'2.05.8(1)-release' ... XAUTHORITY=/home/bozo/.Xauthority _=/etc/bashrc variable22=abc variable23=xzy</TT> 	      </PRE></TD></TR></TABLE>            </P><P>Using <BCLASS="COMMAND">set</B> with the <TTCLASS="OPTION">--</TT>	      option explicitly assigns the contents of a variable to	      the positional parameters. If no variable follows the	      <TTCLASS="OPTION">--</TT> it <ICLASS="FIRSTTERM">unsets</I>	      the positional parameters.</P><DIVCLASS="EXAMPLE"><HR><ANAME="SETPOS"></A><P><B>Example 14-18. Reassigning the positional parameters</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;variable="one two three four five"   4&nbsp;   5&nbsp;set -- $variable   6&nbsp;# Sets positional parameters to the contents of "$variable".   7&nbsp;   8&nbsp;first_param=$1   9&nbsp;second_param=$2  10&nbsp;shift; shift        # Shift past first two positional params.  11&nbsp;# shift 2             also works.  12&nbsp;remaining_params="$*"  13&nbsp;  14&nbsp;echo  15&nbsp;echo "first parameter = $first_param"             # one  16&nbsp;echo "second parameter = $second_param"           # two  17&nbsp;echo "remaining parameters = $remaining_params"   # three four five  18&nbsp;  19&nbsp;echo; echo  20&nbsp;  21&nbsp;# Again.  22&nbsp;set -- $variable  23&nbsp;first_param=$1  24&nbsp;second_param=$2  25&nbsp;echo "first parameter = $first_param"             # one  26&nbsp;echo "second parameter = $second_param"           # two  27&nbsp;  28&nbsp;# ======================================================  29&nbsp;  30&nbsp;set --  31&nbsp;# Unsets positional parameters if no variable specified.  32&nbsp;  33&nbsp;first_param=$1  34&nbsp;second_param=$2  35&nbsp;echo "first parameter = $first_param"             # (null value)  36&nbsp;echo "second parameter = $second_param"           # (null value)  37&nbsp;  38&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>See also <AHREF="loops.html#EX22A">Example 10-2</A> and <AHREF="extmisc.html#EX33A">Example 15-54</A>.</P></DD><DT><ANAME="UNSETREF"></A><BCLASS="COMMAND">unset</B></DT><DD><P>The <BCLASS="COMMAND">unset</B> command deletes a	      shell variable, effectively setting it to	      <ICLASS="FIRSTTERM">null</I>. Note that this command does	      not affect positional parameters.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>unset PATH</B></TT>  <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo $PATH</B></TT> <TTCLASS="COMPUTEROUTPUT">&#13;</TT> <TTCLASS="PROMPT">bash$ </TT></PRE></TD></TR></TABLE>	    </P><DIVCLASS="EXAMPLE"><HR><ANAME="UNS"></A><P><B>Example 14-19. <SPANCLASS="QUOTE">"Unsetting"</SPAN> a variable</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# unset.sh: Unsetting a variable.   3&nbsp;   4&nbsp;variable=hello                       # Initialized.   5&nbsp;echo "variable = $variable"   6&nbsp;   7&nbsp;unset variable                       # Unset.   8&nbsp;                                     # Same effect as:  variable=   9&nbsp;echo "(unset) variable = $variable"  # $variable is null.  10&nbsp;  11&nbsp;if [ -z "$variable" ]                # Try a string-length test.  12&nbsp;then  13&nbsp;  echo "\$variable has zero length."  14&nbsp;fi  15&nbsp;  16&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV></DD><DT><ANAME="EXPORTREF"></A><BCLASS="COMMAND">export</B></DT><DD><P><ANAME="EXPORTREF2"></A></P><P>The <BCLASS="COMMAND">export</B>	  	    <ANAME="AEN8304"HREF="#FTN.AEN8304">[2]</A>	  	      command makes available variables to all child processes	      of the running script or shell. One important use	      of the <BCLASS="COMMAND">export</B> command is in <AHREF="files.html#FILESREF1">startup files</A>, to initialize	      and make accessible <AHREF="othertypesv.html#ENVREF">environmental	      variables</A> to subsequent user processes.</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>Unfortunately, <AHREF="gotchas.html#PARCHILDPROBREF">	     there is no way to export variables back to the parent	     process</A>, to the process that called or invoked the	     script or shell.</P></TD></TR></TABLE></DIV><P><ANAME="EXPORTAWK"></A></P><DIVCLASS="EXAMPLE"><HR><ANAME="COLTOTALER3"></A><P><B>Example 14-20. Using <ICLASS="FIRSTTERM">export</I> to pass a variable to an	      embedded <ICLASS="FIRSTTERM">awk</I> script</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;#  Yet another version of the "column totaler" script (col-totaler.sh)   4&nbsp;#+ that adds up a specified column (of numbers) in the target file.   5&nbsp;#  This uses the environment to pass a script variable to 'awk' . . .   6&nbsp;#+ and places the awk script in a variable.   7&nbsp;   8&nbsp;   9&nbsp;ARGS=2  10&nbsp;E_WRONGARGS=65  11&nbsp;  12&nbsp;if [ $# -ne "$ARGS" ] # Check for proper no. of command line args.  13&nbsp;then  14&nbsp;   echo "Usage: `basename $0` filename column-number"  15&nbsp;   exit $E_WRONGARGS  16&nbsp;fi  17&nbsp;  18&nbsp;filename=$1  19&nbsp;column_number=$2  20&nbsp;  21&nbsp;#===== Same as original script, up to this point =====#  22&nbsp;  23&nbsp;export column_number  24&nbsp;# Export column number to environment, so it's available for retrieval.  25&nbsp;  26&nbsp;  27&nbsp;# -----------------------------------------------  28&nbsp;awkscript='{ total += $ENVIRON["column_number"] }  29&nbsp;END { print total }'  30&nbsp;# Yes, a variable can hold an awk script.  31&nbsp;# -----------------------------------------------  32&nbsp;  33&nbsp;# Now, run the awk script.  34&nbsp;awk "$awkscript" "$filename"  35&nbsp;  36&nbsp;# Thanks, Stephane Chazelas.  37&nbsp;  38&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><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>It is possible to initialize and export		  variables in the same operation, as in <BCLASS="COMMAND">export		  var1=xxx</B>.</P><P>However, as Greg Keraunen points out, in certain		  situations this may have a different effect than		  setting a variable, then exporting it.</P><P>	        <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>export var=(a b); echo ${var[0]}</B></TT> <TTCLASS="COMPUTEROUTPUT">(a b)</TT>    <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>var=(a b); export var; echo ${var[0]}</B></TT> <TTCLASS="COMPUTEROUTPUT">a</TT> 	      </PRE></TD></TR></TABLE>	    </P></TD></TR></TABLE></DIV></DD><DT><ANAME="DECLARE2REF"></A><BCLASS="COMMAND">declare</B>, <BCLASS="COMMAND">typeset</B></DT><DD><P>The <AHREF="declareref.html">declare</A> and	      <AHREF="declareref.html">typeset</A> commands specify	      and/or restrict properties of variables.</P></DD><DT><ANAME="READONLYREF"></A><BCLASS="COMMAND">readonly</B></DT><DD><P>Same as <AHREF="declareref.html">declare -r</A>,	      sets a variable as read-only, or, in effect, as a	      constant. Attempts to change the variable fail with	      an error message. This is the shell analog of the	      <ICLASS="FIRSTTERM">C</I> language <BCLASS="COMMAND">const</B>	      type qualifier.</P></DD><DT><ANAME="GETOPTSX"></A><BCLASS="COMMAND">getopts</B></DT><DD><P>This powerful tool parses command-line arguments passed	      to the script. This is the Bash analog of the <AHREF="extmisc.html#GETOPTY">getopt</A> external command and the	      <ICLASS="FIRSTTERM">getopt</I> library function familiar to	      <ICLASS="FIRSTTERM">C</I> programmers. It permits passing	      and concatenating multiple options	      <ANAME="AEN8391"HREF="#FTN.AEN8391">[3]</A>	      and associated arguments to a script (for	      example <TTCLASS="USERINPUT"><B>scriptname -abc -e	      /usr/local</B></TT>).</P><P><ANAME="GETOPTSOPT"></A></P><P>The <BCLASS="COMMAND">getopts</B> construct uses two implicit	      variables. <TTCLASS="VARNAME">$OPTIND</TT> is the argument	      pointer (<ICLASS="WORDASWORD">OPTion INDex</I>)	      and <TTCLASS="VARNAME">$OPTARG</TT> (<ICLASS="WORDASWORD">OPTion	      ARGument</I>) the (optional) argument attached	      to an option. A colon following the option name in the	      declaration tags that option as having an associated	      argument.</P><P>A <BCLASS="COMMAND">getopts</B> construct usually comes	      packaged in a <AHREF="loops.html#WHILELOOPREF">while	      loop</A>, which processes the options and	      arguments one at a time, then increments the implicit	      <TTCLASS="VARNAME">$OPTIND</TT> variable to step to the	      next.</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>		<OLTYPE="1"><LI><P>The arguments passed from the command line to		      the script must be preceded by a		      minus (<TTCLASS="OPTION">-</TT>). It is the		      prefixed <TTCLASS="OPTION">-</TT> that lets		      <BCLASS="COMMAND">getopts</B> recognize command-line		      arguments as <ICLASS="FIRSTTERM">options</I>.		      In fact, <BCLASS="COMMAND">getopts</B> will not process		      arguments without the prefixed <TTCLASS="OPTION">-</TT>,		      and will terminate option processing at the first		      argument encountered lacking them.</P></LI><LI><P>The <BCLASS="COMMAND">getopts</B> template		      differs slightly from the standard <BCLASS="COMMAND">while</B>		      loop, in that it lacks condition brackets.</P></LI><LI><P>The <BCLASS="COMMAND">getopts</B> construct replaces		     the deprecated <AHREF="extmisc.html#GETOPTY">getopt</A>		     external command.</P></LI></OL>	      </P></TD></TR></TABLE></DIV><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;while getopts ":abcde:fg" Option   2&nbsp;# Initial declaration.   3&nbsp;# a, b, c, d, e, f, and g are the options (flags) expected.   4&nbsp;# The : after option 'e' shows it will have an argument passed with it.   5&nbsp;do   6&nbsp;  case $Option in   7&nbsp;    a ) # Do something with variable 'a'.   8&nbsp;    b ) # Do something with variable 'b'.   9&nbsp;    ...  10&nbsp;    e)  # Do something with 'e', and also with $OPTARG,  11&nbsp;        # which is the associated argument passed with option 'e'.  12&nbsp;    ...  13&nbsp;    g ) # Do something with variable 'g'.  14&nbsp;  esac  15&nbsp;done  16&nbsp;shift $(($OPTIND - 1))  17&nbsp;# Move argument pointer to next.  18&nbsp;  19&nbsp;# All this is not nearly as complicated as it looks &#60;grin&#62;.</PRE></TD></TR></TABLE></P><DIVCLASS="EXAMPLE"><HR><ANAME="EX33"></A><P><B>Example 14-21. Using <ICLASS="FIRSTTERM">getopts</I> to read the	        options/arguments passed to a script</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# Exercising getopts and OPTIND

⌨️ 快捷键说明

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