📄 ch44_22.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 44] 44.22 Finding a Program Name; Multiple Program Names </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:06Z"><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_21.htm"TITLE="44.21 Picking a Name for a New Command "><LINKREL="next"HREF="ch44_23.htm"TITLE="44.23 Reading Files with the . and source Commands "></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_21.htm"TITLE="44.21 Picking a Name for a New Command "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 44.21 Picking a Name for a New Command "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_23.htm"TITLE="44.23 Reading Files with the . and source Commands "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 44.23 Reading Files with the . and source Commands "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7150">44.22 Finding a Program Name; Multiple Program Names </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-52021"></A><ACLASS="indexterm"NAME="AUTOID-52024"></A>A UNIX program should use its name as the first word in error messagesit prints.That's important when the program is running in the background or as partof a pipeline - you need to know which program has the problem:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="replaceable"><I>someprog</I></CODE>: quitting: can't read file xxxxxx</PRE></BLOCKQUOTE></P><PCLASS="para">It's tempting to use just the program name in the <EMCLASS="emphasis">echo</EM> commands:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">echo "<CODECLASS="replaceable"><I>someprog</I></CODE>: quitting: can't read file $file" 1>&2</PRE></BLOCKQUOTE></P><PCLASS="para">but if you ever change the program name, it's easy to forget to fix themessages.A better way is to store the program name in a shell variable at the top ofthe script file, and then use the variable in all messages:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">myname=<CODECLASS="replaceable"><I>someprog</I></CODE> ...echo "$myname: quitting: can't read file $file" 1>&2</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-52040"></A>Even better, use the <CODECLASS="literal">$0</CODE> parameter.The shell automatically puts the script's name there.But <CODECLASS="literal">$0</CODE> can have the absolute pathname of the script, such as<EMCLASS="emphasis">/xxx/yyy/bin/someprog</EM>.The<SPANCLASS="link"><EMCLASS="emphasis">basename</EM> (<ACLASS="linkend"HREF="ch45_18.htm"TITLE="Using basename and dirname ">45.18</A>)</SPAN>program fixes this: <EMCLASS="emphasis">basename</EM> strips off the head ofa pathname - everything but the filename.</P><PCLASS="para">For example, if <CODECLASS="literal">$0</CODE> is <EMCLASS="emphasis">/u/ehuser/bin/sendit</EM>, then:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">myname="`basename $0`"</PRE></BLOCKQUOTE></P><PCLASS="para">would put <EMCLASS="emphasis">sendit</EM> into the <EMCLASS="emphasis">myname</EM> shell variable.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-52058"></A><ACLASS="indexterm"NAME="AUTOID-52061"></A><ACLASS="indexterm"NAME="AUTOID-52064"></A><ACLASS="indexterm"NAME="AUTOID-52067"></A>Just as you can make<SPANCLASS="link">links (<ACLASS="linkend"HREF="ch18_03.htm"TITLE="Files with Two or More Names">18.3</A>)</SPAN>to give UNIX files several names,you can use links to<SPANCLASS="link">give your program several names (<ACLASS="linkend"HREF="ch45_13.htm"TITLE="Save Disk Space and Programming: Multiple Names for a Program">45.13</A>)</SPAN>.For instance, see the script named <EMCLASS="emphasis">ll</EM>, <EMCLASS="emphasis">lf</EM>, <EMCLASS="emphasis">lg</EM> (...and so on)in article<ACLASS="xref"HREF="ch16_07.htm"TITLE="ls Shortcuts: ll, lf, lg, etc. ">16.7</A>.Use <CODECLASS="literal">$0</CODE> to get the current name of the program.</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_21.htm"TITLE="44.21 Picking a Name for a New Command "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 44.21 Picking a Name for a New Command "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_23.htm"TITLE="44.23 Reading Files with the . and source Commands "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 44.23 Reading Files with the . and source Commands "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">44.21 Picking a Name for a New Command </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.23 Reading Files with the . and source Commands </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 + -