⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 why-shell.html

📁 Shall高级编程
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Why Shell Programming?</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Introduction"HREF="part1.html"><LINKREL="PREVIOUS"TITLE="Introduction"HREF="part1.html"><LINKREL="NEXT"TITLE="Starting Off With a Sha-Bang"HREF="sha-bang.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="part1.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="sha-bang.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="WHY-SHELL"></A>Chapter 1. Why Shell Programming?</H1><TABLEBORDER="0"WIDTH="100%"CELLSPACING="0"CELLPADDING="0"CLASS="EPIGRAPH"><TR><TDWIDTH="45%">&nbsp;</TD><TDWIDTH="45%"ALIGN="LEFT"VALIGN="TOP"><I><P><I>No programming language is perfect. There is not even a single        best language; there are only languages well suited or perhaps poorly	suited for particular purposes.</I></P><P><I>--Herbert Mayer</I></P></I></TD></TR></TABLE><P>A working knowledge of shell scripting is essential to anyone      wishing to become reasonably proficient at system administration,      even if they do not anticipate ever having to actually write a      script. Consider that as a Linux machine boots up, it executes the      shell scripts in <TTCLASS="FILENAME">/etc/rc.d</TT>      to restore the system configuration and set up services. A detailed      understanding of these startup scripts is important for analyzing      the behavior of a system, and possibly modifying it.</P><P>The craft of scripting is not hard to master,      since the scripts can be built in bite-sized sections and there      is only a fairly small set of shell-specific operators and options      <ANAME="AEN63"HREF="#FTN.AEN63">[1]</A>      to learn. The syntax is simple and straightforward, similar to      that of invoking and chaining together utilities at the command      line, and there are only a few <SPANCLASS="QUOTE">"rules"</SPAN> governing      their use. Most short scripts work right the first time, and      debugging even the longer ones is straightforward.</P><P>A shell script is a <SPANCLASS="QUOTE">"quick-and-dirty"</SPAN> method of      prototyping a complex application.  Getting even a limited subset      of the functionality to work in a script is often a useful      first stage in project development. This way, the structure of      the application can be tested and played with,      and the major pitfalls found before proceeding      to the final coding in <ICLASS="FIRSTTERM">C</I>,      <ICLASS="FIRSTTERM">C++</I>, <ICLASS="FIRSTTERM">Java</I>, or <AHREF="wrapper.html#PERLREF">Perl</A>.</P><P>Shell scripting hearkens back to the classic UNIX philosophy      of breaking complex projects into simpler subtasks, of chaining      together components and utilities. Many consider this a better,      or at least more esthetically pleasing approach to problem solving      than using one of the new generation of high powered all-in-one      languages, such as <ICLASS="FIRSTTERM">Perl</I>, which attempt to      be all things to all people, but at the cost of forcing you to      alter your thinking processes to fit the tool.</P><P>According to <AHREF="biblio.html#MAYERREF">Herbert Mayer</A>,      <SPANCLASS="QUOTE">"a useful language needs arrays, pointers,	and a generic mechanism for building data structures."</SPAN>	By these criteria, shell scripting falls somewhat short of being	<SPANCLASS="QUOTE">"useful."</SPAN> Or, perhaps not. . . .</P><TABLECLASS="SIDEBAR"BORDER="1"CELLPADDING="5"><TR><TD><DIVCLASS="SIDEBAR"><ANAME="AEN79"></A><P>When not to use shell scripts      <UL><LI><P>Resource-intensive tasks, especially where speed is	    a factor (sorting, hashing, recursion            <ANAME="AEN84"HREF="#FTN.AEN84">[2]</A>	    	    ...)</P></LI><LI><P>Procedures involving heavy-duty math operations,	    especially floating point arithmetic, arbitrary	    precision calculations, or complex numbers (use	    <ICLASS="FIRSTTERM">C++</I> or <ICLASS="FIRSTTERM">FORTRAN</I>	    instead)</P></LI><LI><P>Cross-platform portability required (use	    <ICLASS="FIRSTTERM">C</I> or <ICLASS="FIRSTTERM">Java</I>	    instead)</P></LI><LI><P>Complex applications, where structured programming is	    a necessity (type-checking of variables, function	    prototypes, etc.)</P></LI><LI><P>Mission-critical applications upon which you are betting the	     future of the company</P></LI><LI><P>Situations where <SPANCLASS="emphasis"><ICLASS="EMPHASIS">security</I></SPAN> is	    important, where you need to guarantee the integrity of	    your system and protect against intrusion, cracking, and	    vandalism</P></LI><LI><P>Project consists of subcomponents with interlocking	    dependencies</P></LI><LI><P>Extensive file operations required	    (<ICLASS="FIRSTTERM">Bash</I> is limited to serial file access,	    and that only in a particularly clumsy and inefficient	    line-by-line fashion.)</P></LI><LI><P>Need native support for multi-dimensional arrays</P></LI><LI><P>Need data structures, such as linked lists or trees</P></LI><LI><P>Need to generate / manipulate graphics or GUIs</P></LI><LI><P>Need direct access to system hardware</P></LI><LI><P>Need port or <AHREF="devproc.html#SOCKETREF">socket</A> I/O</P></LI><LI><P>Need to use libraries or interface with legacy code</P></LI><LI><P>Proprietary, closed-source applications (Shell scripts	    put the source code right out in the open for all the world	    to see.)</P></LI></UL></P><P>If any of the above applies, consider a more powerful scripting      language -- perhaps <ICLASS="FIRSTTERM">Perl</I>,      <ICLASS="FIRSTTERM">Tcl</I>, <ICLASS="FIRSTTERM">Python</I>,      <ICLASS="FIRSTTERM">Ruby</I> -- or possibly a      compiled language such as <ICLASS="FIRSTTERM">C</I>,      <ICLASS="FIRSTTERM">C++</I>, or <ICLASS="FIRSTTERM">Java</I>. Even      then, prototyping the application as a shell script might still      be a useful development step.</P></DIV></TD></TR></TABLE><P><ANAME="BASHDEF"></A></P><P>We will be using <SPANCLASS="ACRONYM">Bash</SPAN>, an acronym for      <SPANCLASS="QUOTE">"Bourne-Again shell"</SPAN> and a pun on Stephen Bourne's      now classic <ICLASS="FIRSTTERM">Bourne</I> shell. Bash has become      a <ICLASS="FOREIGNPHRASE">de facto</I> standard for shell      scripting on all flavors of UNIX. Most of the principles this      book covers apply equally well to scripting with other shells,      such as the <ICLASS="FIRSTTERM">Korn Shell</I>, from which Bash      derives some of its features,       <ANAME="AEN140"HREF="#FTN.AEN140">[3]</A>      and the <ICLASS="FIRSTTERM">C Shell</I> and its variants. (Note that      <ICLASS="FIRSTTERM">C Shell</I> programming is not recommended due to      certain inherent problems, as pointed out in an October, 1993 <AHREF="http://www.etext.org/Quartz/computer/unix/csh.harmful.gz"TARGET="_top">Usenet      post</A> by Tom Christiansen.)  </P><P>What follows is a tutorial on shell scripting. It relies      heavily on examples to illustrate various features of the shell.      The example scripts work -- they've been tested, insofar as was      possible -- and some of them are even useful in real life. The      reader can play with the actual working code of the examples      in the source archive (<TTCLASS="FILENAME">scriptname.sh</TT> or      <TTCLASS="FILENAME">scriptname.bash</TT>),         <ANAME="AEN150"HREF="#FTN.AEN150">[4]</A>      give them <ICLASS="FIRSTTERM">execute</I> permission      (<TTCLASS="USERINPUT"><B>chmod u+rx scriptname</B></TT>),      then run them to see what happens. Should the source      archive not be available, then cut-and-paste from the <AHREF="http://www.tldp.org/LDP/abs/abs-guide.html.tar.gz"TARGET="_top">HTML</A> or      <AHREF="http://www.tldp.org/LDP/abs/abs-guide.pdf"TARGET="_top">pdf</A>      rendered versions. Be aware that some of the scripts presented here      introduce features before they are explained, and this may require      the reader to temporarily skip ahead for enlightenment.</P><P>Unless otherwise noted, <AHREF="mailto:thegrendel@theriver.com"TARGET="_top">the author</A> of this       book wrote the example scripts that follow.</P></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN63"HREF="why-shell.html#AEN63">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>These are referred to as <AHREF="internal.html#BUILTINREF">builtins</A>, features internal to the        shell.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN84"HREF="why-shell.html#AEN84">[2]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>Although <AHREF="localvar.html#RECURSIONREF0">recursion	      <SPANCLASS="emphasis"><ICLASS="EMPHASIS">is</I></SPAN> possible in a shell script</A>,	      it tends to be slow and its implementation is often	      an <AHREF="recurnolocvar.html#FIBOREF">ugly kludge</A>.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN140"HREF="why-shell.html#AEN140">[3]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>Many of the features of <ICLASS="FIRSTTERM">ksh88</I>,	 and even a few from the updated <ICLASS="FIRSTTERM">ksh93</I>	 have been merged into Bash.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN150"HREF="why-shell.html#AEN150">[4]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>By convention, user-written shell scripts that are	 Bourne shell compliant generally take a name with a	 <TTCLASS="FILENAME">.sh</TT> extension.  System scripts, such as	 those found in <TTCLASS="FILENAME">/etc/rc.d</TT>,	 do not conform to this nomenclature.</P></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="part1.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="sha-bang.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Introduction</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part1.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Starting Off With a Sha-Bang</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -