📄 ch44_04.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 44] 44.4 Testing How Your System Executes Files </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:53:32Z"><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_03.htm"TITLE="44.3 What's a Shell, Anyway? "><LINKREL="next"HREF="ch44_05.htm"TITLE="44.5 Test String Values with Bourne Shell case "></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_03.htm"TITLE="44.3 What's a Shell, Anyway? "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 44.3 What's a Shell, Anyway? "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_05.htm"TITLE="44.5 Test String Values with Bourne Shell case "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 44.5 Test String Values with Bourne Shell case "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7070">44.4 Testing How Your System Executes Files </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-50312"></A><ACLASS="indexterm"NAME="AUTOID-50316"></A><ACLASS="indexterm"NAME="AUTOID-50319"></A>Your version of UNIX may understand the <CODECLASS="literal">#!</CODE> notation.This is a way to tell UNIX which shell should execute the commands in yourfile.[1]If your UNIX doesn't recognize <CODECLASS="literal">#!</CODE>, you'll need to be sure that youknow how to make it read shell scripts using the Bourne shell - regardlessof the shell you use interactively - because most scripts in this bookare for the Bourne shell.</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[1] Actually,you can use <CODECLASS="literal">#!</CODE> to<SPANCLASS="link">specify any interpreter program (<ACLASS="linkend"HREF="ch45_03.htm"TITLE="Don't Need a Shell for Your Script? Don't Use One ">45.3</A>)</SPAN>,not just a shell.</P></BLOCKQUOTE><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-50329"></A>To test your system, let's make a two-line file named <EMCLASS="emphasis">testing</EM>.</P><BLOCKQUOTECLASS="note"><PCLASS="para"><STRONG>NOTE:</STRONG> Do not make programs named <EMCLASS="emphasis">test</EM>.There's an important system command named<SPANCLASS="link"><EMCLASS="emphasis">test</EM> (<ACLASS="linkend"HREF="ch44_20.htm"TITLE="test: Testing Files and Strings ">44.20</A>)</SPAN>,and your commandmight be used, accidentally, instead of the system program.Name your test programs <EMCLASS="emphasis">testing</EM>, <EMCLASS="emphasis">atest</EM>, whatever - just not<EMCLASS="emphasis">test</EM>.Article<ACLASS="xref"HREF="ch44_21.htm"TITLE="Picking a Name for a New Command ">44.21</A>shows how to find a unique filename.</P></BLOCKQUOTE><OLCLASS="orderedlist"><LICLASS="listitem"><PCLASS="para">Make a file named <EMCLASS="emphasis">testing</EM> (use an editor, or justmake the file by hand with<SPANCLASS="link"><CODECLASS="literal">cat</CODE> <CODECLASS="literal">></CODE> <CODECLASS="literal">testing</CODE> (<ACLASS="linkend"HREF="ch25_02.htm"TITLE="Four Ways to Skin a cat ">25.2</A>)</SPAN>).Put the following two lines in the file.Be sure to start on the <EMCLASS="emphasis">first</EM> line of the file, and type this textjust as it's shown.Be sure that the hash mark (<CODECLASS="literal">#</CODE>) is at the left-hand edge (column 1) of the first line:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">#!/bin/echo justexport stuff</PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para">Exit the editor and save the file.Make the file executable by typing<SPANCLASS="link"><CODECLASS="literal">chmod +x testing</CODE> (<ACLASS="linkend"HREF="ch44_02.htm"TITLE="Writing a Simple Shell Program ">44.2</A>)</SPAN>.</P></LI></OL><PCLASS="para">Now run the program by typing its name at a shell prompt.There are four kinds of responses:</P><OLCLASS="orderedlist"><LICLASS="listitem"><PCLASS="para">If this happens, then the <CODECLASS="literal">#!</CODE> is working.You'll be able to tell your system which shell should run each script:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>testing</B></CODE>just testing%</PRE></BLOCKQUOTE></P><PCLASS="para">The answer <CODECLASS="literal">just ./testing</CODE> also means that <CODECLASS="literal">#!</CODE> is working.If you get an error like "testing: command not found," your currentdirectory may not be in the shell's<SPANCLASS="link">search path (<ACLASS="linkend"HREF="ch08_07.htm"TITLE="Setting Your Search Path ">8.7</A>)</SPAN>;try executing <CODECLASS="literal">./testing</CODE> instead.</P></LI><LICLASS="listitem"><PCLASS="para">If this happens, then your UNIX doesn't understand <CODECLASS="literal">#!</CODE>, but it ranyour program with the Bourne shell anyhow:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>testing</B></CODE>%</PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para">If this happens, then your system ran the program with an older version of theBourne shell.You should not use comment lines starting with a hash mark (<CODECLASS="literal">#</CODE>):</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>testing</B></CODE>#!: not found %</PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para">If this happens, then your UNIX doesn't understand <CODECLASS="literal">#!</CODE>, and itran your program with the C shell:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>testing</B></CODE>export: Command not found.%</PRE></BLOCKQUOTE></P></LI></OL><PCLASS="para">Many UNIX systems, especially newer ones, will answer<CODECLASS="literal">just</CODE> <CODECLASS="literal">testing</CODE> or <CODECLASS="literal">just</CODE> <CODECLASS="literal">./testing</CODE>.That's because, as article<ACLASS="xref"HREF="ch45_05.htm"TITLE="A File That Shows Itself... and What #! Does ">45.5</A>explains,the system strips off the <CODECLASS="literal">#!</CODE> from the startof the line, adds the script file's name (or pathname) to the end of it,and runs it:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/bin/echo just testing</PRE></BLOCKQUOTE></P><PCLASS="para">(Article<ACLASS="xref"HREF="ch08_06.htm"TITLE="Output Command-Line Arguments ">8.6</A>covers the <EMCLASS="emphasis">echo</EM> command.)If your system ran the shell script with the C shell, find away to make it use the Bourne shell instead.Try the trick in article<ACLASS="xref"HREF="ch45_06.htm"TITLE="Making Sure Your Script Runs with Bourne Shell, Without #! ">45.6</A>or ask a local expert such as your system administrator.</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_03.htm"TITLE="44.3 What's a Shell, Anyway? "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 44.3 What's a Shell, Anyway? "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_05.htm"TITLE="44.5 Test String Values with Bourne Shell case "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 44.5 Test String Values with Bourne Shell case "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">44.3 What's a Shell, Anyway? </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.5 Test String Values with Bourne Shell case </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 + -