?? ch20_06.htm
字號:
<html><head><title>Writing Command Lines (UNIX in a Nutshell: System V Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Arnold Robbins" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="1-56592-427-4" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="UNIX in a Nutshell: System V Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Book Title" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch20_05.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch20_07.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">20.6. Writing Command Lines</h2><p><a name="IXT-20-123613" /><a name="IXT-20-123614" />Writing good, portable <tt class="literal">Makefile</tt> filesis a bit of an art.Skill comes with practice and experience.Here are some tips to get you started:</p><ul><li><p>Naming your file <tt class="literal">Makefile</tt> instead of<tt class="literal">makefile</tt> usually causes it to be listed firstwith <tt class="literal">ls</tt>.This makes it easier to find in a directory with many files.</p></li><li><p>Remember that command lines must startwith a leading tab character.You cannot just indent the line with spaces,even eight spaces.If you use spaces, <tt class="literal">make</tt> exits with anunhelpful message about “missing separator characters.”</p></li><li><p>Remember that <tt class="literal">$</tt> is special to <tt class="literal">make</tt>.To get a literal <tt class="literal">$</tt> into your command lines,use <tt class="literal">$$</tt>.This is particularly important if you want to access an environmentvariable that isn't a <tt class="literal">make</tt> macro.Also, if you wish to use the shell's <tt class="literal">$$</tt>for the current process ID, you have to type it as <tt class="literal">$$$$</tt>.</p></li><li><p>Write multiline shell statements, such as shell conditionalsand loops, with trailing semicolons and a trailing backslash:</p><blockquote><pre class="code">if [ -f specfile ] ; then \... ; \else \... ; \fi</pre></blockquote><p>Note that the shell keywords <tt class="literal">then</tt> and <tt class="literal">else</tt>don't need the semicolon.(What happens is that <tt class="literal">make</tt> passes the backslashesand the newlines to the shell.The escaped newlines are not syntactically important, so thesemicolons are needed to separate the different parts of the command.This can be confusing.If you use a semicolon where you would normally put a newlinein a shell script, things should work correctly.)</p></li><li><p>Remember that each line is run in a separate shell.This means that commands that change the shell's environment(such as <tt class="literal">cd</tt>) are ineffective across multiplelines.The correct way to write such commands is to separate commands on thesame line with a semicolon:</p><blockquote><pre class="code">cd subdir; $(MAKE)</pre></blockquote></li><li><p>For guaranteed portability, always set <tt class="literal">SHELL</tt>to <tt class="literal">/bin/sh</tt>.Some versions of <tt class="literal">make</tt> use whatever valueis in the environment for <tt class="literal">SHELL</tt>, unlessit is explicitly set in the <tt class="literal">Makefile</tt>.</p></li><li><p>Use macros for standard commands.<tt class="literal">make</tt> already helps out with this,providing macros such as <tt class="literal">$(CC)</tt>,<tt class="literal">$(YACC)</tt>, and so on.</p></li><li><p>When removing files, start your command line with<tt class="literal">-$(RM)</tt> instead of <tt class="literal">$($RM)</tt>.(The – causes <tt class="literal">make</tt> toignore the exit status of the command.)This way, if the file you were trying to remove doesn'texist, and <tt class="literal">rm</tt> exits with an error,<tt class="literal">make</tt> can keep going.</p></li><li><p>When running subsidiary invocations of <tt class="literal">make</tt>,typically in subdirectories of your main program tree, alwaysuse <tt class="literal">$(MAKE)</tt>, and not <tt class="literal">make</tt>.Lines that contain <tt class="literal">$(MAKE)</tt> are alwaysexecuted, even if <tt class="literal">-n</tt> has been provided,allowing you to test out a whole hierarchy of <tt class="literal">Makefile</tt>files.This does not happen for lines that invoke <tt class="literal">make</tt> directly.</p></li><li><p>Often, it is convenient to organize a large software project intosubprojects, with each one having a subdirectory.The top-level <tt class="literal">Makefile</tt> then justinvokes <tt class="literal">make</tt> in each subdirectory.Here's the way to do it:</p><blockquote><pre class="code">SUBDIRS = proj1 proj2 proj3...projects: $(SUBDIRS) for i in $(SUBDIRS); \ do \ echo ====== Making in $$i ; \ ( cd $$i ; $(MAKE) $(MAKEFLAGS) $@ ) ; \ done</pre></blockquote></li></ul><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch20_05.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch20_07.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">20.5. Special Target Names</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">20.7. Sample Default Macros, Suffixes, and Rules</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2003</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,88,96" href="../index.htm"><area shape="rect" coords="90,0,165,96" href="../upt/index.htm"><area shape="rect" coords="168,1,253,107" href="../mac/index.htm"><area shape="rect" coords="255,0,335,97" href="../korn/index.htm"><area shape="rect" coords="337,0,415,109" href="../unixnut/index.htm"><area shape="rect" coords="417,0,512,122" href="../sedawk/index.htm"><area shape="rect" coords="514,0,605,105" href="../lunix/index.htm"><area shape="rect" coords="611,2,694,121" href="../vi/index.htm"></map></body></html>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -