📄 ch44_19.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 44] 44.19 The Bourne Shell set Command </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:54:00Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch44_01.htm"TITLE="44. Shell Programming for the Uninitiated"><LINKREL="prev"HREF="ch44_18.htm"TITLE="44.18 Standard Command-Line Parsing "><LINKREL="next"HREF="ch44_20.htm"TITLE="44.20 test: Testing Files and Strings "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch44_18.htm"TITLE="44.18 Standard Command-Line Parsing "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 44.18 Standard Command-Line Parsing "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 44<BR>Shell Programming for the Uninitiated</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch44_20.htm"TITLE="44.20 test: Testing Files and Strings "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 44.20 test: Testing Files and Strings "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-6810">44.19 The Bourne Shell set Command </A></H2><PCLASS="para">[Most of this article, except <ICLASS="filename">IFS</I> and <CODECLASS="literal">--</CODE>, also applies to theC shell. -JP]</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-51786"></A><ACLASS="indexterm"NAME="AUTOID-51788"></A>The Bourne shell command line can have options like <EMCLASS="emphasis">-e</EM> (exit ifany command returns non-zero status).It can also have other arguments; these are passed to shell scripts.You can set new command-line parameters while you're typing interactivecommands (at a shell prompt) or in a shell script.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-51793"></A>To reset the command-line parameters, just type <EMCLASS="emphasis">set</EM> followed by the newparameters.So, for example, to ask the shell to show expanded versions of command linesafter you type them, set the<SPANCLASS="link"><EMCLASS="emphasis">-v</EM> (verbose) option (<ACLASS="linkend"HREF="ch08_17.htm"TITLE="verbose and echo Variables Show Quoting ">8.17</A>)</SPAN>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>set -v</B></CODE>$ <CODECLASS="userinput"><B>mail $group1 < message</B></CODE>mail andy ellen heather steve wilma < message$ <CODECLASS="userinput"><B>mail $group2 < message</B></CODE>mail jpeek@jpeek.com randy@xyz.edu yori@mongo.medfly.com < message$ <CODECLASS="userinput"><B>set +v</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Typing <CODECLASS="literal">set +v</CODE> cancels the <EMCLASS="emphasis">v</EM> option on many Bourne shells.</P><PCLASS="para">You can put filenames or any other strings in the command-line parametersinteractively or from a shell script.That's handy for storing and parsing the output of a UNIX command with<SPANCLASS="link">backquotes (<ACLASS="linkend"HREF="ch09_16.htm"TITLE="Command Substitution ">9.16</A>)</SPAN>.For example, you can get a list of all logged-in users from the parameters<CODECLASS="literal">$1</CODE>, <CODECLASS="literal">$2</CODE>, and so on.Use<EMCLASS="emphasis">users</EM>if your system has it.Otherwise, use<SPANCLASS="link"><EMCLASS="emphasis">who</EM> (<ACLASS="linkend"HREF="ch51_04.htm"TITLE="Who's On? ">51.4</A>)</SPAN>-and<SPANCLASS="link"><EMCLASS="emphasis">cut</EM> (<ACLASS="linkend"HREF="ch35_14.htm"TITLE="Cutting Columns or Fields with cut ">35.14</A>)</SPAN>to strip off everything but theusernames:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"> <ACLASS="co"HREF="ch44_16.htm"TITLE="44.16 Handling Command-Line Arguments with a for Loop ">for</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">$ <CODECLASS="userinput"><B>set `users`</B></CODE>$ <CODECLASS="userinput"><B>set `who | cut -c1-8`</B></CODE>$ <CODECLASS="userinput"><B>for u</B></CODE>> <CODECLASS="userinput"><B>do</B></CODE>> <ICLASS="lineannotation">...do something with each user ($u)...</I>> <CODECLASS="userinput"><B>done</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-51827"></A>You can save the original parameters in another variable and reset themlater:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">oldparms="$*"set <CODECLASS="replaceable"><I>something new</I></CODE> <ICLASS="lineannotation">...use new settings...</I>set $oldparms</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-51835"></A><ACLASS="indexterm"NAME="AUTOID-51838"></A>If the first parameter you <EMCLASS="emphasis">set</EM> starts with a dash, like <CODECLASS="literal">-e</CODE>,the shell will treat it as its own option instead of as a string toput into the command-line parameters.<ACLASS="indexterm"NAME="AUTOID-51843"></A>To avoid this, use <CODECLASS="literal">--</CODE> (two dashes) as the first argument to<EMCLASS="emphasis">set</EM>.In this example, <CODECLASS="literal">$1</CODE> gets <EMCLASS="emphasis">-e</EM>, and the filenames expandedfrom the wildcard pattern go into <CODECLASS="literal">$2</CODE>, <CODECLASS="literal">$3</CODE>, etc.:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">set -- -e file*</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-51855"></A><ACLASS="indexterm"NAME="AUTOID-51858"></A><ACLASS="indexterm"NAME="AUTOID-51861"></A><ACLASS="indexterm"NAME="AUTOID-51864"></A><ACLASS="indexterm"NAME="AUTOID-51867"></A><ACLASS="indexterm"NAME="AUTOID-51870"></A>Because the shell parses and scans the new parameters before it stores them,<SPANCLASS="link">wildcards (<ACLASS="linkend"HREF="ch15_02.htm"TITLE="Filename Wildcards in a Nutshell ">15.2</A>)</SPAN>and other<SPANCLASS="link">special characters (<ACLASS="linkend"HREF="ch08_19.htm"TITLE='"Special" Characters and Operators '>8.19</A>)</SPAN>will be interpreted - watch your<SPANCLASS="link">quoting (<ACLASS="linkend"HREF="ch08_14.htm"TITLE="Bourne Shell Quoting ">8.14</A>)</SPAN>.You can take advantage of this to parse lines of text into pieces thataren't separated with the usual spaces and TABs - for instance, a line from adatabase with colon-separated fields - by setting the<SPANCLASS="link"><EMCLASS="emphasis">IFS</EM> (<ACLASS="linkend"HREF="ch35_21.htm"TITLE="Using IFS to Split Strings ">35.21</A>)</SPAN>variable before the <EMCLASS="emphasis">set</EM> command.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-51880"></A>If you want to save any special quoting on the original command line,be careful; the quoting will be lost unless you're clever.For example, if <CODECLASS="literal">$1</CODE> used to be <EMCLASS="emphasis">John Smith</EM>, it'll be splitafter it's restored: <CODECLASS="literal">$1</CODE> will have <EMCLASS="emphasis">John</EM> and <CODECLASS="literal">$2</CODE> will be<EMCLASS="emphasis">Smith</EM>.A bettersolution might be to use a<SPANCLASS="link">subshell (<ACLASS="linkend"HREF="ch13_07.htm"TITLE="The () Subshell Operators ">13.7</A>)</SPAN>for the part of the script where you need to reset the command-lineparameters:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"># reset command-line parameters during subshell only:(set <CODECLASS="replaceable"><I>some new parameters</I></CODE> ...<EMCLASS="emphasis">do something with new parameters</EM>...)# original parameters aren't affected from here on...</PRE></BLOCKQUOTE></P><PCLASS="para">One last note: <EMCLASS="emphasis">set</EM> won't set <CODECLASS="literal">$0</CODE>, the name of the script file.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch44_18.htm"TITLE="44.18 Standard Command-Line Parsing "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 44.18 Standard Command-Line Parsing "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch44_20.htm"TITLE="44.20 test: Testing Files and Strings "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 44.20 test: Testing Files and Strings "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">44.18 Standard Command-Line Parsing </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">44.20 test: Testing Files and Strings </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -