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

📄 ch01_05.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 1] 1.5 Anyone Can Program the Shell </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:30:58Z"><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="ch01_01.htm"TITLE="1. Introduction"><LINKREL="prev"HREF="ch01_04.htm"TITLE="1.4 Using Pipes to Create a New Tool "><LINKREL="next"HREF="ch01_06.htm"TITLE="1.6 Power Tools for Editing "></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="ch01_04.htm"TITLE="1.4 Using Pipes to Create a New Tool "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 1.4 Using Pipes to Create a New Tool "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 1<BR>Introduction</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch01_06.htm"TITLE="1.6 Power Tools for Editing "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 1.6 Power Tools for Editing "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1003">1.5 Anyone Can Program the Shell </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-895"></A>One of the really wonderful things about the shell is that itdoesn't just read and execute the commands you type at a prompt.The shell is a complete programming language.</P><PCLASS="para">The ease of shell programming is one of the realhighlights of UNIX for novices.A shell program need be no more than a single complex commandline saved in a file&nbsp;- or a series of commands.</P><PCLASS="para">For example, let's say that you occasionally need to convert aMacintosh Microsoft Word file for use on your UNIX system.<ACLASS="indexterm"NAME="AUTOID-900"></A>Word lets you save the file in ASCII format. But there's acatch: the Macintosh uses a carriage return(<SPANCLASS="link">ASCII character 015 (<ACLASS="linkend"HREF="ch51_03.htm"TITLE="ASCII Characters: Listing and Getting Values ">51.3</A>)</SPAN>)to mark the end of each line, while UNIX uses a linefeed(ASCII 012). As a result, with UNIX, the file lookslike one long paragraph, with no end in sight.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-905"></A>That's easy to fix: the UNIX<SPANCLASS="link"><EMCLASS="emphasis">tr</EM> (<ACLASS="linkend"HREF="ch35_11.htm"TITLE="Hacking on Characters with tr ">35.11</A>)</SPAN>command can convert everyoccurrence of one character in a file to another:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>tr '\015' '\012' &lt;</B></CODE> <CODECLASS="replaceable"><I>file.mac</I></CODE><CODECLASS="userinput"><B> &gt; </B></CODE><CODECLASS="replaceable"><I>file.UNIX</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-917"></A><ACLASS="indexterm"NAME="AUTOID-920"></A><ACLASS="indexterm"NAME="AUTOID-922"></A>But you're a novice, and you don't want to remember thisparticular piece of magic. Fine. Save the first part ofthis command line in a file called <EMCLASS="emphasis">mac2UNIX</EM> in your<SPANCLASS="link">personal <EMCLASS="emphasis">bin</EM> directory (<ACLASS="linkend"HREF="ch04_02.htm"TITLE="A bin Directory for Your Programs and Scripts ">4.2</A>)</SPAN>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">tr '\015' '\012'</PRE></BLOCKQUOTE></P><PCLASS="para">Make the file executable with<SPANCLASS="link"><EMCLASS="emphasis">chmod</EM> (<ACLASS="linkend"HREF="ch22_07.htm"TITLE="Using chmod to Change File Permission ">22.7</A>)</SPAN>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>chmod +x mac2UNIX</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Now you can say:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>mac2UNIX &lt; </B></CODE><CODECLASS="replaceable"><I>file.mac</I></CODE><CODECLASS="userinput"><B> &gt; </B></CODE><CODECLASS="replaceable"><I>file.UNIX</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">But say, why settle for that? What if you want to convert abunch of files at once? Easy. The shell includes a general<ACLASS="indexterm"NAME="AUTOID-945"></A><ACLASS="indexterm"NAME="AUTOID-948"></A><ACLASS="indexterm"NAME="AUTOID-950"></A>way of referring to arguments passed to a script, and a numberof looping constructs. The script:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch44_16.htm"TITLE="44.16 Handling Command-Line Arguments with a for Loop ">for</A> <ACLASS="co"HREF="ch06_08.htm"TITLE="6.8 Shell Variables ">$x</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">for xdo    echo &quot;Converting $x&quot;    tr '\015' '\012' &lt; &quot;$x&quot; &gt; &quot;tmp.$x&quot;    mv &quot;tmp.$x&quot; &quot;$x&quot;done</PRE></TD></TR></TABLE></P><PCLASS="para">will convert any number of files with one command, replacingeach original with the converted version:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>mac2UNIX</B></CODE> <CODECLASS="replaceable"><I>file1 file2 file3 ...</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">As you become more familiarwith UNIX, it quickly becomes apparent that doingjust a little homework can save hours of tedium.This script incorporates only two simple programming constructs: the<EMCLASS="emphasis">for</EM> loop<ACLASS="indexterm"NAME="AUTOID-964"></A><ACLASS="indexterm"NAME="AUTOID-966"></A>and<SPANCLASS="link">variable substitution (<ACLASS="linkend"HREF="ch06_08.htm"TITLE="Shell Variables ">6.8</A>, <ACLASS="linkend"HREF="ch06_01.htm#UPT-ART-1170"TITLE="What Environment Variables Are Good For ">6.1</A>)</SPAN>.As a new user, with no programmingexperience, I learned these two constructs by example: I saved a skeleton<EMCLASS="emphasis">for</EM> loop in a file and simplyfilled in the blanks with whatevercommands I wanted to repeat.</P><PCLASS="para">Simple shell programs like this did more for my feeling that computers couldautomate my work than anything since my first introduction to word processing.It made real sense of the line, &quot;Let the computer do the dirty work.&quot;</P><PCLASS="para">In short, UNIX is sometimes difficult because it is so rich and complex.The user who doesn't want to learn the complexity doesn't have to&nbsp;- thebasic housekeeping commands are as simple as MS-DOS on the IBM PC.But the user who wants to take the time to investigate the possibilitiescan uncover a wealth of useful tools.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">TOR</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="ch01_04.htm"TITLE="1.4 Using Pipes to Create a New Tool "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 1.4 Using Pipes to Create a New Tool "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="ch01_06.htm"TITLE="1.6 Power Tools for Editing "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 1.6 Power Tools for Editing "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">1.4 Using Pipes to Create a New Tool </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">1.6 Power Tools for Editing </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 &amp; 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 + -