📄 ch09_14.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 9] 9.14 Using Here Documents for Form Letters, etc. </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:35:08Z"><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="ch09_01.htm"TITLE="9. Saving Time on the Command Line"><LINKREL="prev"HREF="ch09_13.htm"TITLE="9.13 Multiline Commands, Secondary Prompts "><LINKREL="next"HREF="ch09_15.htm"TITLE="9.15 Throwaway Scripts for Complicated Commands "></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="ch09_13.htm"TITLE="9.13 Multiline Commands, Secondary Prompts "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 9.13 Multiline Commands, Secondary Prompts "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 9<BR>Saving Time on the Command Line</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch09_15.htm"TITLE="9.15 Throwaway Scripts for Complicated Commands "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 9.15 Throwaway Scripts for Complicated Commands "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0238">9.14 Using Here Documents for Form Letters, etc. </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-10540"></A><ACLASS="indexterm"NAME="AUTOID-10542"></A>The<SPANCLASS="link">here document operator <CODECLASS="literal"><<</CODE> (<ACLASS="linkend"HREF="ch08_18.htm"TITLE="Here Documents ">8.18</A>)</SPAN>is used in shell scripts.It tells the shell to take lines from the script as standard input to a command.The example below shows<SPANCLASS="link">a loop (<ACLASS="linkend"HREF="ch45_17.htm"TITLE="Making a for Loop with Multiple Variables ">45.17</A>)</SPAN>that prints three nasty form letters with the<SPANCLASS="link"><EMCLASS="emphasis">lpr</EM> (<ACLASS="linkend"HREF="ch43_02.htm"TITLE="Introduction to Printing on UNIX ">43.2</A>)</SPAN>command.Each letter has a different person's name and the current date at the top.You can put this loop into a<SPANCLASS="link">shell script (<ACLASS="linkend"HREF="ch44_02.htm"TITLE="Writing a Simple Shell Program ">44.2</A>)</SPAN>or just type it in<SPANCLASS="link">at a Bourne shell prompt (<ACLASS="linkend"HREF="ch09_12.htm"TITLE="The Bourne Shell for Loop ">9.12</A>, <ACLASS="linkend"HREF="ch09_13.htm"TITLE="Multiline Commands, Secondary Prompts ">9.13</A>)</SPAN>.Each line of the loop body starts with a TAB character, which the<ACLASS="indexterm"NAME="AUTOID-10551"></A><CODECLASS="literal"><<-</CODE> operator removes before the printer gets the text:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">for person in "Mary Smith" "Doug Jones" "Alison Eddy"do lpr <<- ENDMSG `date` Dear $person, This is your last notice. Buy me pizza tonight or else I'll type "rm -r *" when you're not looking. This is not a joak. Signed, The midnight skulker ENDMSGdone</PRE></BLOCKQUOTE></P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="xref.warning"HREF="ch45_26.htm"TITLE="45.26 Quoted hereis Document Terminators: sh vs. csh ">Warning!</A> </TH><TDVALIGN="TOP"> This loop runs three <EMCLASS="emphasis">lpr</EM> commands; each form letter prints on aseparate page.The shell reads the standard input until it finds theterminator word, which in this case is <CODECLASS="literal">ENDMSG</CODE>.The word (<CODECLASS="literal">ENDMSG</CODE>) has to be on a line all by itself.(Some Bourne shells don't have the <CODECLASS="literal"><<-</CODE> operator to removeleading TAB characters.In that case, use <CODECLASS="literal"><<</CODE> and don't indent the loop body.)The<SPANCLASS="link">backquotes (<ACLASS="linkend"HREF="ch09_16.htm"TITLE="Command Substitution ">9.16</A>)</SPAN>run the<SPANCLASS="link"><EMCLASS="emphasis">date</EM> (<ACLASS="linkend"HREF="ch51_10.htm"TITLE="The date Command ">51.10</A>)</SPAN>command and output its date;<CODECLASS="literal">$person</CODE> is replaced with the person's name set at the top of theloop.The rest of the text is copied as is to the standard input of the <EMCLASS="emphasis">lpr</EM>command.</TD></TR></TABLE><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</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="ch09_13.htm"TITLE="9.13 Multiline Commands, Secondary Prompts "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 9.13 Multiline Commands, Secondary Prompts "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="ch09_15.htm"TITLE="9.15 Throwaway Scripts for Complicated Commands "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 9.15 Throwaway Scripts for Complicated Commands "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">9.13 Multiline Commands, Secondary Prompts </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">9.15 Throwaway Scripts for Complicated Commands </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 & 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 + -