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

📄 othertypesv.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!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.76b+"><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"><TABLESUMMARY="Header navigation table"WIDTH="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"ACCESSKEY="P">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"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="OTHERTYPESV"></A>4.4. Special Variable Types</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 <AHREF="special-chars.html#PROCESSREF">process</A> 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 <AHREF="special-chars.html#PROCESSREF">processes</A>,	      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.	      <SPANCLASS="emphasis"><ICLASS="EMPHASIS"><AHREF="internal.html#FORKREF">Child processes</A>	      cannot export variables back to the parent processes that	      spawned them.</I></SPAN></P><P>Definition: A <ICLASS="FIRSTTERM">child process</I>	      is a subprocess launched by another process, its	      <ICLASS="FIRSTTERM">parent</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><ANAME="SCRNAMEPARAM"></A><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="AEN2193"HREF="#FTN.AEN2193">[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 <SPANCLASS="emphasis"><ICLASS="EMPHASIS">all</I></SPAN> 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

⌨️ 快捷键说明

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