📄 internal.html
字号:
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 #!/bin/bash 2 3 variable="one two three four five" 4 5 set -- $variable 6 # Sets positional parameters to the contents of "$variable". 7 8 first_param=$1 9 second_param=$2 10 shift; shift # Shift past first two positional params. 11 # shift 2 also works. 12 remaining_params="$*" 13 14 echo 15 echo "first parameter = $first_param" # one 16 echo "second parameter = $second_param" # two 17 echo "remaining parameters = $remaining_params" # three four five 18 19 echo; echo 20 21 # Again. 22 set -- $variable 23 first_param=$1 24 second_param=$2 25 echo "first parameter = $first_param" # one 26 echo "second parameter = $second_param" # two 27 28 # ====================================================== 29 30 set -- 31 # Unsets positional parameters if no variable specified. 32 33 first_param=$1 34 second_param=$2 35 echo "first parameter = $first_param" # (null value) 36 echo "second parameter = $second_param" # (null value) 37 38 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"> </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 #!/bin/bash 2 # unset.sh: Unsetting a variable. 3 4 variable=hello # Initialized. 5 echo "variable = $variable" 6 7 unset variable # Unset. 8 # Same effect as: variable= 9 echo "(unset) variable = $variable" # $variable is null. 10 11 if [ -z "$variable" ] # Try a string-length test. 12 then 13 echo "\$variable has zero length." 14 fi 15 16 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 #!/bin/bash 2 3 # Yet another version of the "column totaler" script (col-totaler.sh) 4 #+ that adds up a specified column (of numbers) in the target file. 5 # This uses the environment to pass a script variable to 'awk' . . . 6 #+ and places the awk script in a variable. 7 8 9 ARGS=2 10 E_WRONGARGS=65 11 12 if [ $# -ne "$ARGS" ] # Check for proper no. of command line args. 13 then 14 echo "Usage: `basename $0` filename column-number" 15 exit $E_WRONGARGS 16 fi 17 18 filename=$1 19 column_number=$2 20 21 #===== Same as original script, up to this point =====# 22 23 export column_number 24 # Export column number to environment, so it's available for retrieval. 25 26 27 # ----------------------------------------------- 28 awkscript='{ total += $ENVIRON["column_number"] } 29 END { print total }' 30 # Yes, a variable can hold an awk script. 31 # ----------------------------------------------- 32 33 # Now, run the awk script. 34 awk "$awkscript" "$filename" 35 36 # Thanks, Stephane Chazelas. 37 38 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 while getopts ":abcde:fg" Option 2 # Initial declaration. 3 # a, b, c, d, e, f, and g are the options (flags) expected. 4 # The : after option 'e' shows it will have an argument passed with it. 5 do 6 case $Option in 7 a ) # Do something with variable 'a'. 8 b ) # Do something with variable 'b'. 9 ... 10 e) # Do something with 'e', and also with $OPTARG, 11 # which is the associated argument passed with option 'e'. 12 ... 13 g ) # Do something with variable 'g'. 14 esac 15 done 16 shift $(($OPTIND - 1)) 17 # Move argument pointer to next. 18 19 # All this is not nearly as complicated as it looks <grin>.</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 #!/bin/bash 2 # Exercising getopts and OPTIND
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -