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

📄 othertypesv.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Special Variable Types</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.57"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Introduction to Variables and Parameters"HREF="variables.html"><LINKREL="PREVIOUS"TITLE="Bash Variables Are Untyped"HREF="untyped.html"><LINKREL="NEXT"TITLE="Quoting"HREF="quoting.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"><TABLEWIDTH="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="untyped.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 4. Introduction to Variables and Parameters</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="quoting.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="OTHERTYPESV">4.4. Special Variable Types</A></H1><DIVCLASS="VARIABLELIST"><DL><DT><TTCLASS="REPLACEABLE"><I>local variables</I></TT></DT><DD><P>variables visible only within a <AHREF="special-chars.html#CODEBLOCKREF">code block</A> or function	      (see also <AHREF="localvar.html#LOCALREF">local variables</A>	      in <AHREF="functions.html#FUNCTIONREF">functions</A>)</P></DD><DT><ANAME="ENVREF"></A><TTCLASS="REPLACEABLE"><I>environmental variables</I></TT></DT><DD><P>variables that affect the behavior of the shell and	      user interface</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>In a more general context, each process has an		<SPANCLASS="QUOTE">"environment"</SPAN>, that is, a group of		variables that hold information that the process		may reference. In this sense, the shell behaves like		any other process.</P><P>Every time a shell starts, it creates shell variables that		correspond to its own environmental variables. Updating		or adding new environmental variables causes the shell		to update its environment, and all the shell's child		processes (the commands it executes) inherit this		environment.</P></TD></TR></TABLE></DIV><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 space allotted to the environment is limited.	        Creating too many environmental variables or ones that use up		excessive space may cause problems.</P><P>	          <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>eval "`seq 10000 | sed -e 's/.*/export var&#38;=ZZZZZZZZZZZZZZ/'`"</B></TT>  <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>du</B></TT> <TTCLASS="COMPUTEROUTPUT">bash: /usr/bin/du: Argument list too long</TT> 	          </PRE></TD></TR></TABLE>	      </P><P>(Thank you, St閜hane Chazelas for the clarification,	        and for providing the above example.)</P></TD></TR></TABLE></DIV><P>If a script sets environmental variables, they need to be	      <SPANCLASS="QUOTE">"exported"</SPAN>, that is, reported to the	      environment local to the script. This is the function of	      the <AHREF="internal.html#EXPORTREF">export</A> command.</P><ANAME="CHILDREF"></A><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>A script can <BCLASS="COMMAND">export</B> variables only	      to child processes, that is, only to commands or processes	      which that particular script initiates. A script invoked	      from the command line <TTCLASS="REPLACEABLE"><I>cannot</I></TT>	      export variables back to the command line environment.	      <ICLASS="EMPHASIS"><AHREF="internal.html#FORKREF">Child processes</A> cannot export	      variables back to the parent processes that spawned	      them.</I></P></TD></TR></TABLE></DIV><P>---</P></DD><DT><ANAME="POSPARAMREF1"></A><TTCLASS="REPLACEABLE"><I>positional parameters</I></TT></DT><DD><P>arguments passed to the script from the command	      line: <TTCLASS="VARNAME">$0</TT>, <TTCLASS="VARNAME">$1</TT>,	      <TTCLASS="VARNAME">$2</TT>, <TTCLASS="VARNAME">$3</TT> . . .</P><P><TTCLASS="VARNAME">$0</TT> is the name of the script itself,	      <TTCLASS="VARNAME">$1</TT> is the first argument,	      <TTCLASS="VARNAME">$2</TT> the second, <TTCLASS="VARNAME">$3</TT>	      the third, and so forth.	      <ANAME="AEN1872"HREF="#FTN.AEN1872">[1]</A>	      <ANAME="BRACKETNOTATION"></A>	      After <TTCLASS="VARNAME">$9</TT>, the arguments must be enclosed	      in brackets, for example, <TTCLASS="VARNAME">${10}</TT>,	      <TTCLASS="VARNAME">${11}</TT>, <TTCLASS="VARNAME">${12}</TT>.</P><P>The special variables <AHREF="variables2.html#APPREF">$* and $@</A>	      denote <ICLASS="EMPHASIS">all</I> the positional parameters.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX17"></A><P><B>Example 4-5. Positional Parameters</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;# Call this script with at least 10 parameters, for example   4&nbsp;# ./scriptname 1 2 3 4 5 6 7 8 9 10   5&nbsp;MINPARAMS=10   6&nbsp;   7&nbsp;echo   8&nbsp;   9&nbsp;echo "The name of this script is \"$0\"."  10&nbsp;# Adds ./ for current directory  11&nbsp;echo "The name of this script is \"`basename $0`\"."  12&nbsp;# Strips out path name info (see 'basename')  13&nbsp;  14&nbsp;echo  15&nbsp;  16&nbsp;if [ -n "$1" ]              # Tested variable is quoted.  17&nbsp;then  18&nbsp; echo "Parameter #1 is $1"  # Need quotes to escape #  19&nbsp;fi   20&nbsp;  21&nbsp;if [ -n "$2" ]  22&nbsp;then  23&nbsp; echo "Parameter #2 is $2"  24&nbsp;fi   25&nbsp;  26&nbsp;if [ -n "$3" ]  27&nbsp;then  28&nbsp; echo "Parameter #3 is $3"  29&nbsp;fi   30&nbsp;  31&nbsp;# ...  32&nbsp;  33&nbsp;  34&nbsp;if [ -n "${10}" ]  # Parameters &#62; $9 must be enclosed in {brackets}.  35&nbsp;then  36&nbsp; echo "Parameter #10 is ${10}"  37&nbsp;fi   38&nbsp;  39&nbsp;echo "-----------------------------------"  40&nbsp;echo "All the command-line parameters are: "$*""  41&nbsp;  42&nbsp;if [ $# -lt "$MINPARAMS" ]  43&nbsp;then  44&nbsp;  echo  45&nbsp;  echo "This script needs at least $MINPARAMS command-line arguments!"  46&nbsp;fi    47&nbsp;  48&nbsp;echo  49&nbsp;  50&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ICLASS="FIRSTTERM">Bracket notation</I> for positional	      parameters leads to a fairly simple way of referencing	      the <ICLASS="EMPHASIS">last</I> 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 12-2</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 null variable	      assignment, 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. <BCLASS="COMMAND">wh</B>, <AHREF="communications.html#WHOISREF">whois</A> 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	      <ICLASS="EMPHASIS"><TTCLASS="VARNAME">$0</TT> (the script name)	      does not change</I>. 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 <BCLASS="COMMAND">shift</B></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# Using 'shift' to step through all the positional parameters.   3&nbsp;   4&nbsp;#  Name this script something like shft,   5&nbsp;#+ and invoke it with some parameters, for example   6&nbsp;#          ./shft a b c def 23 skidoo   7&nbsp;   8&nbsp;until [ -z "$1" ]  # Until all parameters used up...   9&nbsp;do  10&nbsp;  echo -n "$1 "  11&nbsp;  shift  12&nbsp;done  13&nbsp;  14&nbsp;echo               # Extra line feed.  15&nbsp;  16&nbsp;exit 0</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">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.AEN1872"HREF="othertypesv.html#AEN1872">[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 manpage for		<BCLASS="COMMAND">execv</B>.</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="untyped.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="quoting.html">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">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 + -