📄 ch45_02.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 45] 45.2 The Story of : # #! </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:20Z"><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_01.htm"TITLE="45.1 Beyond the Basics "><LINKREL="next"HREF="ch45_03.htm"TITLE="45.3 Don't Need a Shell for Your Script? Don't Use One "></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_01.htm"TITLE="45.1 Beyond the Basics "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 45.1 Beyond the Basics "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_03.htm"TITLE="45.3 Don't Need a Shell for Your Script? Don't Use One "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 45.3 Don't Need a Shell for Your Script? Don't Use One "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0375">45.2 The Story of : # #! </A></H2><PCLASS="para">Once upon a time, there was the Bourne shell. Since there was only "the"shell, there was no trouble deciding how to run a script: run itwith <EMCLASS="emphasis">the</EM> shell. It worked, and everyone was happy.</P><PCLASS="para">Along came progress, and wrote another shell. The people thoughtthis was good, for now they could choose their own shell. So somechose the one, and some the other, and they wrote shell scripts andwere happy. But one day someone who used the "other" shell rana script by someone who used the "other other" shell, and alas! itbombed spectacularly. The people wailed and called upon theirGuru for help.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-52239"></A><ACLASS="indexterm"NAME="AUTOID-52242"></A><ACLASS="indexterm"NAME="AUTOID-52245"></A><ACLASS="indexterm"NAME="AUTOID-52248"></A>"Well," said the Guru, "I see the problem. The one shell and theother are not compatible. We need to make sure that the shells knowwhich other shell to use to run each script. And lo! the one shellhas a `comment' called <CODECLASS="literal">:</CODE>, and the other a true comment called<CODECLASS="literal">#</CODE>. I hereby decree that henceforth, the one shell will runscripts that start with <CODECLASS="literal">:</CODE>, and the other those that start with<CODECLASS="literal">#</CODE>." And it was so, and the people were happy.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-52256"></A><ACLASS="indexterm"NAME="AUTOID-52259"></A>But progress was not finished. This time he noticed that onlyshells ran scripts, and thought that if the kernel too could runscripts, that this would be good, and the people would be happy.So he wrote more code, and now the kernel could run scripts,but only if they began with the magic incantation: <CODECLASS="literal">#!</CODE>, andtold the kernel which shell ran the script. And it was so, andthe people were confused.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-52264"></A>For the <CODECLASS="literal">#!</CODE> looked like a "comment." Though the kernel could seethe <CODECLASS="literal">#!</CODE> and run a shell, it would not do so unless certain magicbits were set. And if the incantation were mispronounced, thattoo could stop the kernel, which, after all, was not omniscient.And so the people wailed, but alas!, the Guru did not respond.And so it was, and still it is today.Anyway, you will get best results from a 4BSD machine by using</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">#! /bin/sh</PRE></BLOCKQUOTE></P><PCLASS="para">or</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">#! /bin/csh</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-52275"></A><ACLASS="indexterm"NAME="AUTOID-52278"></A><ACLASS="indexterm"NAME="AUTOID-52281"></A>as the first line of your script.<CODECLASS="literal">#!</CODE> <CODECLASS="literal">/bin/csh -f</CODE> is also helpfulon occasion [it's usually faster because <EMCLASS="emphasis">csh</EM> won't read your<SPANCLASS="link"><EMCLASS="emphasis">.cshrc</EM> file (<ACLASS="linkend"HREF="ch02_02.htm"TITLE="Shell Setup Files-Which, Where, and Why ">2.2</A>)</SPAN>. <EMCLASS="emphasis">-JP</EM> ].</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">CT</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_01.htm"TITLE="45.1 Beyond the Basics "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 45.1 Beyond the Basics "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_03.htm"TITLE="45.3 Don't Need a Shell for Your Script? Don't Use One "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 45.3 Don't Need a Shell for Your Script? Don't Use One "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">45.1 Beyond the Basics </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.3 Don't Need a Shell for Your Script? Don't Use One </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 + -