📄 ch45_13.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 45] 45.13 Save Disk Space and Programming: Multiple Names for a Program</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:36Z"><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="ch45_01.htm"TITLE="45. Shell Programming for the Initiated"><LINKREL="prev"HREF="ch45_12.htm"TITLE="45.12 Parameter Substitution "><LINKREL="next"HREF="ch45_14.htm"TITLE="45.14 Finding the Last Command-Line Argument "></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="ch45_12.htm"TITLE="45.12 Parameter Substitution "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 45.12 Parameter Substitution "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 45<BR>Shell Programming for the Initiated</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch45_14.htm"TITLE="45.14 Finding the Last Command-Line Argument "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 45.14 Finding the Last Command-Line Argument "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-6190">45.13 Save Disk Space and Programming: Multiple Names for a Program</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-53166"></A><ACLASS="indexterm"NAME="AUTOID-53169"></A><ACLASS="indexterm"NAME="AUTOID-53172"></A><ACLASS="indexterm"NAME="AUTOID-53175"></A><ACLASS="indexterm"NAME="AUTOID-53178"></A><ACLASS="indexterm"NAME="AUTOID-53181"></A>If you're writing:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">several programs that do the same kinds of things,</P></LI><LICLASS="listitem"><PCLASS="para">programs that use a lot of the same code (as you're writing the second,third, etc., programs, you copy a lot of lines from the first program), or</P></LI><LICLASS="listitem"><PCLASS="para">a program with several options that make a big change in the way it works,</P></LI></UL><PCLASS="para">you might want to write just one program and make<SPANCLASS="link">links (<ACLASS="linkend"HREF="ch18_04.htm"TITLE="More About Links ">18.4</A>, <ACLASS="linkend"HREF="ch18_03.htm"TITLE="Files with Two or More Names">18.3</A>)</SPAN>to it instead.The program can find the name you called it with and, through <EMCLASS="emphasis">case</EM> or<EMCLASS="emphasis">test</EM> commands, work in different ways.For instance, the Berkeley UNIX commands <EMCLASS="emphasis">ex</EM>, <EMCLASS="emphasis">vi</EM>, <EMCLASS="emphasis">view</EM>,<EMCLASS="emphasis">edit</EM>, and others are all links to the same executable file.This takes less disk space and makes maintenance easier.It's usually only sensible when most of the code is the same in eachprogram.If the program is full of name tests and lots of separate code,this technique may be more trouble than it's worth.</P><PCLASS="para">Depending on how the script program is called, this name can bea simplerelative pathname like <CODECLASS="literal">prog</CODE> or <CODECLASS="literal">./prog</CODE>-it can also be anabsolute pathname like <CODECLASS="literal">/usr/joe/bin/prog</CODE>(article<ACLASS="xref"HREF="ch14_02.htm"TITLE="Using Relative and Absolute Pathnames ">14.2</A>explains pathnames).There are a couple of ways to handle this in a shell script.If there's just one main piece of code in the script, as in the<SPANCLASS="link"><EMCLASS="emphasis">lf</EM> script (<ACLASS="linkend"HREF="ch16_07.htm"TITLE="ls Shortcuts: ll, lf, lg, etc. ">16.7</A>)</SPAN>,a <EMCLASS="emphasis">case</EM> that tests <CODECLASS="literal">$0</CODE> might be best.The asterisk (<CODECLASS="literal">*</CODE>) wildcard at the start of each case(see article<ACLASS="xref"HREF="ch44_06.htm"TITLE="Pattern Matching in case Statements ">44.6</A>)handles thedifferent pathnames that might be used to call the script:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">case "$0" in*<CODECLASS="replaceable"><I>name1</I></CODE>) <ICLASS="lineannotation">...do this when called as name1...</I> ;;*<CODECLASS="replaceable"><I>name2</I></CODE>) <ICLASS="lineannotation">...do this when called as name2...</I> ;; ...*) <ICLASS="lineannotation">...print error and exit if $0 doesn't match...</I> ;;esac</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-53218"></A>You might also want to use<SPANCLASS="link"><EMCLASS="emphasis">basename</EM> (<ACLASS="linkend"HREF="ch45_18.htm"TITLE="Using basename and dirname ">45.18</A>)</SPAN>to strip off any leading pathname and store the cleaned-up <CODECLASS="literal">$0</CODE> in avariable called <EMCLASS="emphasis">myname</EM>.You can test <CODECLASS="literal">$myname</CODE> anywhere in the script and also use it for errormessages:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">myname=`basename $0` ...case "$myname" in ...echo "$myname: aborting; error in xxxxxx" 1>&2 ...</PRE></BLOCKQUOTE></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="ch45_12.htm"TITLE="45.12 Parameter Substitution "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 45.12 Parameter Substitution "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="ch45_14.htm"TITLE="45.14 Finding the Last Command-Line Argument "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 45.14 Finding the Last Command-Line Argument "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">45.12 Parameter Substitution </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">45.14 Finding the Last Command-Line Argument </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 + -