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

📄 ch44_01.htm

📁 the unix power tools
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 44] Shell Programming for the Uninitiated</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:53:22Z"><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="part08.htm"TITLE="VIII. Shell Programming "><LINKREL="prev"HREF="part08.htm"TITLE="VIII. Shell Programming "><LINKREL="next"HREF="ch44_02.htm"TITLE="44.2 Writing a Simple Shell Program "></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="PART"HREF="part08.htm"TITLE="VIII. Shell Programming "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: VIII. Shell Programming "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 44</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch44_02.htm"TITLE="44.2 Writing a Simple Shell Program "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 44.2 Writing a Simple Shell Program "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="CHAPTER"><H1CLASS="chapter"><ACLASS="title"NAME="UPT-CHP-44">44. Shell Programming for the Uninitiated</A></H1><DIVCLASS="htmltoc"><P><B>Contents:</B><BR><ACLASS="sect1"HREF="#UPT-ART-0065"TITLE="44.1 Everyone Should Learn Some Shell Programming ">Everyone Should Learn Some Shell Programming </A><BR><ACLASS="sect1"HREF="ch44_02.htm"TITLE="44.2 Writing a Simple Shell Program ">Writing a Simple Shell Program </A><BR><ACLASS="sect1"HREF="ch44_03.htm"TITLE="44.3 What's a Shell, Anyway? ">What's a Shell, Anyway? </A><BR><ACLASS="sect1"HREF="ch44_04.htm"TITLE="44.4 Testing How Your System Executes Files ">Testing How Your System Executes Files </A><BR><ACLASS="sect1"HREF="ch44_05.htm"TITLE="44.5 Test String Values with Bourne Shell case ">Test String Values with Bourne Shell case </A><BR><ACLASS="sect1"HREF="ch44_06.htm"TITLE="44.6 Pattern Matching in case Statements ">Pattern Matching in case Statements </A><BR><ACLASS="sect1"HREF="ch44_07.htm"TITLE="44.7 Exit Status of UNIX Processes ">Exit Status of UNIX Processes </A><BR><ACLASS="sect1"HREF="ch44_08.htm"TITLE="44.8 Test Exit Status with the if Statement ">Test Exit Status with the if Statement </A><BR><ACLASS="sect1"HREF="ch44_09.htm"TITLE="44.9 Testing Your Success ">Testing Your Success </A><BR><ACLASS="sect1"HREF="ch44_10.htm"TITLE="44.10 Loops That Test Exit Status ">Loops That Test Exit Status </A><BR><ACLASS="sect1"HREF="ch44_11.htm"TITLE="44.11 Set Exit Status of a Shell (Script) ">Set Exit Status of a Shell (Script) </A><BR><ACLASS="sect1"HREF="ch44_12.htm"TITLE="44.12 Trapping Exits Caused by Interrupts ">Trapping Exits Caused by Interrupts </A><BR><ACLASS="sect1"HREF="ch44_13.htm"TITLE="44.13 read: Reading from the Keyboard ">read: Reading from the Keyboard </A><BR><ACLASS="sect1"HREF="ch44_14.htm"TITLE="44.14 Putting awk, sed, etc., Inside Shell Scripts ">Putting awk, sed, etc., Inside Shell Scripts </A><BR><ACLASS="sect1"HREF="ch44_15.htm"TITLE="44.15 Handling Command-Line Arguments in Shell Scripts ">Handling Command-Line Arguments in Shell Scripts </A><BR><ACLASS="sect1"HREF="ch44_16.htm"TITLE="44.16 Handling Command-Line Arguments with a for Loop ">Handling Command-Line Arguments with a for Loop </A><BR><ACLASS="sect1"HREF="ch44_17.htm"TITLE="44.17 Handling Arguments with while and shift ">Handling Arguments with while and shift </A><BR><ACLASS="sect1"HREF="ch44_18.htm"TITLE="44.18 Standard Command-Line Parsing ">Standard Command-Line Parsing </A><BR><ACLASS="sect1"HREF="ch44_19.htm"TITLE="44.19 The Bourne Shell set Command ">The Bourne Shell set Command </A><BR><ACLASS="sect1"HREF="ch44_20.htm"TITLE="44.20 test: Testing Files and Strings ">test: Testing Files and Strings </A><BR><ACLASS="sect1"HREF="ch44_21.htm"TITLE="44.21 Picking a Name for a New Command ">Picking a Name for a New Command </A><BR><ACLASS="sect1"HREF="ch44_22.htm"TITLE="44.22 Finding a Program Name; Multiple Program Names ">Finding a Program Name; Multiple Program Names </A><BR><ACLASS="sect1"HREF="ch44_23.htm"TITLE="44.23 Reading Files with the . and source Commands ">Reading Files with the . and source Commands </A></P><P></P></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0065">44.1 Everyone Should Learn Some Shell Programming </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-49912"></A><ACLASS="indexterm"NAME="AUTOID-49914"></A><ACLASS="indexterm"NAME="AUTOID-49917"></A>One of the great things about UNIX is that it's made up of individualutilities, &quot;building blocks&quot; like <EMCLASS="emphasis">cat</EM> and <EMCLASS="emphasis">grep</EM>, that you runfrom a shell prompt.Using pipes, redirection, filters, and so on, you can combine thoseutilities to do an incredible number of things.Shell programming lets you take the same commands you'd type at a shellprompt&nbsp;- and put them into a file you can run by just typing its name.You can make new programs that combine UNIX programs (and other shellscripts) in your own way to do exactly what you need.If you don't like the way a program works, you can write a shell scriptto do just what you want.</P><PCLASS="para">Because many UNIX users use the shell every day, they don't need tolearn a whole new language for programming... just some tips andtechniques.In fact, this chapter covers a lot of programming techniques thatyou'll want to use even when you aren'tprogramming. For example, loops and tests are handy on thecommand line.</P><PCLASS="para">(This series of articles does assume that you've written programs in some language before, or are generally familiar withprogramming concepts.If you haven't, you might start with a more comprehensive shellprogramming book.)</P><PCLASS="para">Some of the topics you need to learn about as a beginning shellprogrammer have already been covered in other chapters. Here are the articles you'll probably want to read&nbsp;- in an orderthat makes sense if you're looking for something of a tutorial:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">To see how to write a simple shell program, article<ACLASS="xref"HREF="ch44_02.htm"TITLE="Writing a Simple Shell Program ">44.2</A>.To embed scripts from other languages like <EMCLASS="emphasis">sed</EM> and <EMCLASS="emphasis">awk</EM> ina shell script, article<ACLASS="xref"HREF="ch44_14.htm"TITLE="Putting awk, sed, etc., Inside Shell Scripts ">44.14</A>.</P></LI><LICLASS="listitem"><PCLASS="para">For explanation of shells in general, article<ACLASS="xref"HREF="ch44_03.htm"TITLE="What's a Shell, Anyway? ">44.3</A>.</P></LI><LICLASS="listitem"><PCLASS="para">To test how your system executes files so you'll know how to write yourshell programs, article<ACLASS="xref"HREF="ch44_04.htm"TITLE="Testing How Your System Executes Files ">44.4</A>.</P></LI><LICLASS="listitem"><PCLASS="para">To read about environment and shell variables, articles<ACLASS="xref"HREF="ch06_01.htm#UPT-ART-1170"TITLE="What Environment Variables Are Good For ">6.1</A>and<ACLASS="xref"HREF="ch06_08.htm"TITLE="Shell Variables ">6.8</A>,respectively.</P></LI><LICLASS="listitem"><PCLASS="para">The <EMCLASS="emphasis">echo</EM> command is covered by article<ACLASS="xref"HREF="ch08_06.htm"TITLE="Output Command-Line Arguments ">8.6</A>.</P></LI><LICLASS="listitem"><PCLASS="para">Shell quoting is explained in article<ACLASS="xref"HREF="ch08_14.htm"TITLE="Bourne Shell Quoting ">8.14</A>.</P></LI><LICLASS="listitem"><PCLASS="para">Test strings with a <EMCLASS="emphasis">case</EM> statement, article<ACLASS="xref"HREF="ch44_05.htm"TITLE="Test String Values with Bourne Shell case ">44.5</A>.Match patterns in a <EMCLASS="emphasis">case</EM> statement, article<ACLASS="xref"HREF="ch44_06.htm"TITLE="Pattern Matching in case Statements ">44.6</A>.</P></LI><LICLASS="listitem"><PCLASS="para">Use the output of one command as arguments to another command

⌨️ 快捷键说明

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