📄 ch45_01.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 45] Shell Programming for the Initiated</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:54:15Z"><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="ch44_23.htm"TITLE="44.23 Reading Files with the . and source Commands "><LINKREL="next"HREF="ch45_02.htm"TITLE="45.2 The Story of : # #! "></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_23.htm"TITLE="44.23 Reading Files with the . and source Commands "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 44.23 Reading Files with the . and source Commands "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 45</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch45_02.htm"TITLE="45.2 The Story of : # #! "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 45.2 The Story of : # #! "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="CHAPTER"><H1CLASS="chapter"><ACLASS="title"NAME="UPT-CHP-45">45. Shell Programming for the Initiated</A></H1><DIVCLASS="htmltoc"><P><B>Contents:</B><BR><ACLASS="sect1"HREF="#UPT-ART-7970"TITLE="45.1 Beyond the Basics ">Beyond the Basics </A><BR><ACLASS="sect1"HREF="ch45_02.htm"TITLE="45.2 The Story of : # #! ">The Story of : # #! </A><BR><ACLASS="sect1"HREF="ch45_03.htm"TITLE="45.3 Don't Need a Shell for Your Script? Don't Use One ">Don't Need a Shell for Your Script? Don't Use One </A><BR><ACLASS="sect1"HREF="ch45_04.htm"TITLE="45.4 Fun with #! ">Fun with #! </A><BR><ACLASS="sect1"HREF="ch45_05.htm"TITLE="45.5 A File That Shows Itself... and What #! Does ">A File That Shows Itself... and What #! Does </A><BR><ACLASS="sect1"HREF="ch45_06.htm"TITLE="45.6 Making Sure Your Script Runs with Bourne Shell, Without #! ">Making Sure Your Script Runs with Bourne Shell, Without #! </A><BR><ACLASS="sect1"HREF="ch45_07.htm"TITLE="45.7 The exec Command ">The exec Command </A><BR><ACLASS="sect1"HREF="ch45_08.htm"TITLE="45.8 Handling Signals to Child Processes ">Handling Signals to Child Processes </A><BR><ACLASS="sect1"HREF="ch45_09.htm"TITLE='45.9 The Unappreciated Bourne Shell ":" Operator'>The Unappreciated Bourne Shell ":" Operator</A><BR><ACLASS="sect1"HREF="ch45_10.htm"TITLE="45.10 Removing a File Once It's Opened - for Security and Easy Cleanup">Removing a File Once It's Opened - for Security and Easy Cleanup</A><BR><ACLASS="sect1"HREF="ch45_11.htm"TITLE="45.11 The Multipurpose jot Command ">The Multipurpose jot Command </A><BR><ACLASS="sect1"HREF="ch45_12.htm"TITLE="45.12 Parameter Substitution ">Parameter Substitution </A><BR><ACLASS="sect1"HREF="ch45_13.htm"TITLE="45.13 Save Disk Space and Programming: Multiple Names for a Program">Save Disk Space and Programming: Multiple Names for a Program</A><BR><ACLASS="sect1"HREF="ch45_14.htm"TITLE="45.14 Finding the Last Command-Line Argument ">Finding the Last Command-Line Argument </A><BR><ACLASS="sect1"HREF="ch45_15.htm"TITLE="45.15 How to Unset all Command-Line Parameters ">How to Unset all Command-Line Parameters </A><BR><ACLASS="sect1"HREF="ch45_16.htm"TITLE="45.16 Standard Input to a for Loop ">Standard Input to a for Loop </A><BR><ACLASS="sect1"HREF="ch45_17.htm"TITLE="45.17 Making a for Loop with Multiple Variables ">Making a for Loop with Multiple Variables </A><BR><ACLASS="sect1"HREF="ch45_18.htm"TITLE="45.18 Using basename and dirname ">Using basename and dirname </A><BR><ACLASS="sect1"HREF="ch45_19.htm"TITLE="45.19 A while Loop with Several Loop Control Commands ">A while Loop with Several Loop Control Commands </A><BR><ACLASS="sect1"HREF="ch45_20.htm"TITLE="45.20 Overview: Open Files and File Descriptors ">Overview: Open Files and File Descriptors </A><BR><ACLASS="sect1"HREF="ch45_21.htm"TITLE="45.21 n>&m: Swap Standard Output and Standard Error ">n>&m: Swap Standard Output and Standard Error </A><BR><ACLASS="sect1"HREF="ch45_22.htm"TITLE="45.22 Handling Files Line-by-Line ">Handling Files Line-by-Line </A><BR><ACLASS="sect1"HREF="ch45_23.htm"TITLE="45.23 The Ins and Outs of Redirected I/O Loops ">The Ins and Outs of Redirected I/O Loops </A><BR><ACLASS="sect1"HREF="ch45_24.htm"TITLE="45.24 A Shell Can Read a Script from its Standard Input, But...">A Shell Can Read a Script from its Standard Input, But...</A><BR><ACLASS="sect1"HREF="ch45_25.htm"TITLE="45.25 Shell Scripts On-the-Fly from Standard Input ">Shell Scripts On-the-Fly from Standard Input </A><BR><ACLASS="sect1"HREF="ch45_26.htm"TITLE="45.26 Quoted hereis Document Terminators: sh vs. csh ">Quoted hereis Document Terminators: sh vs. csh </A><BR><ACLASS="sect1"HREF="ch45_27.htm"TITLE='45.27 Turn Off echo for "Secret" Answers'>Turn Off echo for "Secret" Answers</A><BR><ACLASS="sect1"HREF="ch45_28.htm"TITLE="45.28 Quick Reference: expr ">Quick Reference: expr </A><BR><ACLASS="sect1"HREF="ch45_29.htm"TITLE="45.29 Testing Characters in a String with expr ">Testing Characters in a String with expr </A><BR><ACLASS="sect1"HREF="ch45_30.htm"TITLE="45.30 Grabbing Parts of a String ">Grabbing Parts of a String </A><BR><ACLASS="sect1"HREF="ch45_31.htm"TITLE="45.31 Nested Command Substitution ">Nested Command Substitution </A><BR><ACLASS="sect1"HREF="ch45_32.htm"TITLE="45.32 A Better read Command: grabchars ">A Better read Command: grabchars </A><BR><ACLASS="sect1"HREF="ch45_33.htm"TITLE="45.33 Testing Two Strings with One case Statement ">Testing Two Strings with One case Statement </A><BR><ACLASS="sect1"HREF="ch45_34.htm"TITLE="45.34 Arrays in the Bourne Shell ">Arrays in the Bourne Shell </A><BR><ACLASS="sect1"HREF="ch45_35.htm"TITLE="45.35 Using a Control Character in a Script ">Using a Control Character in a Script </A><BR><ACLASS="sect1"HREF="ch45_36.htm"TITLE="45.36 Shell Lockfile ">Shell Lockfile </A></P><P></P></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7970">45.1 Beyond the Basics </A></H2><PCLASS="para">This chapter has a bunch of tricks and techniques for programmingwith the Bourne shell.Some of them are documented but hard to find; others aren't documentedat all.Here is a summary of this chapter's articles:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">The first group of articles is about <BCLASS="emphasis.bold">making a file directly executablewith </B><CODECLASS="userinput"><B>#!</B></CODE> on the first line.On many versions of UNIX (see article<ACLASS="xref"HREF="ch44_04.htm"TITLE="Testing How Your System Executes Files ">44.4</A>),anexecutable filecan start with a first line like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">#!<CODECLASS="replaceable"><I>/path/to/interpreter</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">The kernel will start the program named in that line and give it thefile to read.Chris Torek's Usenet classic, article<ACLASS="xref"HREF="ch45_02.htm"TITLE="The Story of : # #! ">45.2</A>,explains how <CODECLASS="literal">#!</CODE> started.Article<ACLASS="xref"HREF="ch45_03.htm"TITLE="Don't Need a Shell for Your Script? Don't Use One ">45.3</A>explains that your "shell scripts" may not need a shell at all.Article<ACLASS="xref"HREF="ch45_04.htm"TITLE="Fun with #! ">45.4</A>will give you a few grins as it shows unusual examples of <CODECLASS="literal">#!</CODE>-andarticle<ACLASS="xref"HREF="ch45_05.htm"TITLE="A File That Shows Itself... and What #! Does ">45.5</A>has experiments to help you understand what <CODECLASS="literal">#!</CODE> does.If your UNIX doesn't have <CODECLASS="literal">#!</CODE>, the trick in article<ACLASS="xref"HREF="ch45_06.htm"TITLE="Making Sure Your Script Runs with Bourne Shell, Without #! ">45.6</A>will let you be sure your scripts run with the Bourne shell.</P><PCLASS="para">Scripts using an interpreter that isn't a shell are in articles<ACLASS="xref"HREF="ch25_11.htm"TITLE="crush: A cat that Skips all Blank Lines ">25.11</A>,<ACLASS="xref"HREF="ch25_12.htm"TITLE="Double Space, Triple Space ... ">25.12</A>,and<ACLASS="xref"HREF="ch35_08.htm"TITLE="Centering Lines in a File ">35.8</A>.</P></LI><LICLASS="listitem"><PCLASS="para">The next five articles are about <BCLASS="emphasis.bold">processes and commands</B>.The <EMCLASS="emphasis">exec</EM> command, article<ACLASS="xref"HREF="ch45_07.htm"TITLE="The exec Command ">45.7</A>,replaces the shell with another process; it can also be used to changeinput/output redirection (see below).The <EMCLASS="emphasis">trap</EM> command can control how signals are passed to child processes;see article<ACLASS="xref"HREF="ch45_08.htm"TITLE="Handling Signals to Child Processes ">45.8</A>.The <CODECLASS="literal">:</CODE> (colon) operator evaluates its arguments and returns azero status - article<ACLASS="xref"HREF="ch45_09.htm"TITLE='The Unappreciated Bourne Shell ":" Operator'>45.9</A>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -