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

📄 here-docs.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Here Documents</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Advanced Topics"HREF="part5.html"><LINKREL="PREVIOUS"TITLE="Globbing"HREF="globbingref.html"><LINKREL="NEXT"TITLE="I/O Redirection"HREF="io-redirection.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="globbingref.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="io-redirection.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="HERE-DOCS"></A>Chapter 18. Here Documents</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>Here and now, boys.</I></P><P><I>--Aldous Huxley, <BCLASS="COMMAND">Island</B></I></P></I></TD></TR></TABLE><P><ANAME="HEREDOCREF"></A></P><P>A <ICLASS="FIRSTTERM">here document</I> is a special-purpose	code block. It uses a form of <AHREF="io-redirection.html#IOREDIRREF">I/O	redirection</A> to feed a command list to	an interactive program or a command, such as <AHREF="communications.html#FTPREF">ftp</A>, <AHREF="external.html#CATREF">cat</A>,	or the <ICLASS="FIRSTTERM">ex</I> text editor.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;COMMAND &#60;&#60;InputComesFromHERE   2&nbsp;...   3&nbsp;InputComesFromHERE</PRE></TD></TR></TABLE></P><P><ANAME="LIMITSTRINGREF"></A></P><P>A <ICLASS="FIRSTTERM">limit string</I> delineates (frames)	the command list.  The special symbol <SPANCLASS="TOKEN">&#60;&#60;</SPAN> designates	the limit string.  This has the effect of redirecting the output	of a file into the <TTCLASS="FILENAME">stdin</TT> of the program	or command. It is similar to <TTCLASS="USERINPUT"><B>interactive-program &#60;	command-file</B></TT>, where <TTCLASS="FILENAME">command-file</TT>	contains	  <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;command #1   2&nbsp;command #2   3&nbsp;...</PRE></TD></TR></TABLE></P><P>The <ICLASS="FIRSTTERM">here document</I> alternative looks        like this:</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;interactive-program &#60;&#60;LimitString   3&nbsp;command #1   4&nbsp;command #2   5&nbsp;...   6&nbsp;LimitString</PRE></TD></TR></TABLE></P><P>Choose a <ICLASS="FIRSTTERM">limit string</I> sufficiently	unusual that it will not occur anywhere in the command list and	confuse matters.</P><P>Note that <ICLASS="FIRSTTERM">here documents</I> may sometimes	be used to good effect with non-interactive utilities and commands,	such as, for example, <AHREF="system.html#WALLREF">wall</A>.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX70"></A><P><B>Example 18-1. <ICLASS="FIRSTTERM">broadcast</I>: Sends message to everyone	logged in</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;wall &#60;&#60;zzz23EndOfMessagezzz23   4&nbsp;E-mail your noontime orders for pizza to the system administrator.   5&nbsp;    (Add an extra dollar for anchovy or mushroom topping.)   6&nbsp;# Additional message text goes here.   7&nbsp;# Note: 'wall' prints comment lines.   8&nbsp;zzz23EndOfMessagezzz23   9&nbsp;  10&nbsp;# Could have been done more efficiently by  11&nbsp;#         wall &#60;message-file  12&nbsp;#  However, embedding the message template in a script  13&nbsp;#+ is a quick-and-dirty one-off solution.  14&nbsp;  15&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="VIHERE"></A></P><P>Even such unlikely candidates as the <ICLASS="FIRSTTERM">vi</I>        text editor lend themselves to <ICLASS="FIRSTTERM">here        documents</I>.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX69"></A><P><B>Example 18-2. <ICLASS="FIRSTTERM">dummyfile</I>: Creates a 2-line dummy	file</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;# Non-interactive use of 'vi' to edit a file.   4&nbsp;# Emulates 'sed'.   5&nbsp;   6&nbsp;E_BADARGS=65   7&nbsp;   8&nbsp;if [ -z "$1" ]   9&nbsp;then  10&nbsp;  echo "Usage: `basename $0` filename"  11&nbsp;  exit $E_BADARGS  12&nbsp;fi  13&nbsp;  14&nbsp;TARGETFILE=$1  15&nbsp;  16&nbsp;# Insert 2 lines in file, then save.  17&nbsp;#--------Begin here document-----------#  18&nbsp;vi $TARGETFILE &#60;&#60;x23LimitStringx23  19&nbsp;i  20&nbsp;This is line 1 of the example file.  21&nbsp;This is line 2 of the example file.  22&nbsp;^[  23&nbsp;ZZ  24&nbsp;x23LimitStringx23  25&nbsp;#----------End here document-----------#  26&nbsp;  27&nbsp;#  Note that ^[ above is a literal escape  28&nbsp;#+ typed by Control-V &#60;Esc&#62;.  29&nbsp;  30&nbsp;#  Bram Moolenaar points out that this may not work with 'vim',  31&nbsp;#+ because of possible problems with terminal interaction.  32&nbsp;  33&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>        The above script could just as effectively have been implemented with	<BCLASS="COMMAND">ex</B>, rather than	<BCLASS="COMMAND">vi</B>. <ANAME="EXSCRIPTREF"></A><ICLASS="FIRSTTERM">Here	documents</I> containing a list of <BCLASS="COMMAND">ex</B>	commands are common enough to form their own category, known as	<ICLASS="FIRSTTERM">ex scripts</I>.	  <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;#  Replace all instances of "Smith" with "Jones"   3&nbsp;#+ in files with a ".txt" filename suffix.    4&nbsp;   5&nbsp;ORIGINAL=Smith   6&nbsp;REPLACEMENT=Jones   7&nbsp;   8&nbsp;for word in $(fgrep -l $ORIGINAL *.txt)   9&nbsp;do  10&nbsp;  # -------------------------------------  11&nbsp;  ex $word &#60;&#60;EOF  12&nbsp;  :%s/$ORIGINAL/$REPLACEMENT/g  13&nbsp;  :wq  14&nbsp;EOF  15&nbsp;  # :%s is the "ex" substitution command.  16&nbsp;  # :wq is write-and-quit.  17&nbsp;  # -------------------------------------  18&nbsp;done</PRE></TD></TR></TABLE>	</P><P><ANAME="CATSCRIPTREF"></A></P><P>Analogous to <SPANCLASS="QUOTE">"ex scripts"</SPAN> are <ICLASS="FIRSTTERM">cat        scripts</I>.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX71"></A><P><B>Example 18-3. Multi-line message using <ICLASS="FIRSTTERM">cat</I></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;#  'echo' is fine for printing single line messages,   4&nbsp;#+  but somewhat problematic for for message blocks.   5&nbsp;#   A 'cat' here document overcomes this limitation.   6&nbsp;   7&nbsp;cat &#60;&#60;End-of-message   8&nbsp;-------------------------------------   9&nbsp;This is line 1 of the message.  10&nbsp;This is line 2 of the message.  11&nbsp;This is line 3 of the message.  12&nbsp;This is line 4 of the message.  13&nbsp;This is the last line of the message.  14&nbsp;-------------------------------------  15&nbsp;End-of-message  16&nbsp;  17&nbsp;#  Replacing line 7, above, with  18&nbsp;#+   cat &#62; $Newfile &#60;&#60;End-of-message  19&nbsp;#+       ^^^^^^^^^^  20&nbsp;#+ writes the output to the file $Newfile, rather than to stdout.  21&nbsp;  22&nbsp;exit 0  23&nbsp;  24&nbsp;  25&nbsp;#--------------------------------------------  26&nbsp;# Code below disabled, due to "exit 0" above.  27&nbsp;  28&nbsp;# S.C. points out that the following also works.  29&nbsp;echo "-------------------------------------  30&nbsp;This is line 1 of the message.  31&nbsp;This is line 2 of the message.  32&nbsp;This is line 3 of the message.  33&nbsp;This is line 4 of the message.  34&nbsp;This is the last line of the message.  35&nbsp;-------------------------------------"  36&nbsp;# However, text may not include double quotes unless they are escaped.</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="LIMITSTRDASH"></A></P><P>The <TTCLASS="OPTION">-</TT> option to mark a here document limit string	(<TTCLASS="USERINPUT"><B>&#60;&#60;-LimitString</B></TT>) suppresses leading	tabs (but not spaces) in the output. This may be useful in making	a script more readable.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX71A"></A><P><B>Example 18-4. Multi-line message, with tabs suppressed</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# Same as previous example, but...   3&nbsp;   4&nbsp;#  The - option to a here document &#60;&#60;-   5&nbsp;#+ suppresses leading tabs in the body of the document,   6&nbsp;#+ but *not* spaces.   7&nbsp;   8&nbsp;cat &#60;&#60;-ENDOFMESSAGE   9&nbsp;	This is line 1 of the message.  10&nbsp;	This is line 2 of the message.  11&nbsp;	This is line 3 of the message.  12&nbsp;	This is line 4 of the message.  13&nbsp;	This is the last line of the message.  14&nbsp;ENDOFMESSAGE  15&nbsp;# The output of the script will be flush left.  16&nbsp;# Leading tab in each line will not show.  17&nbsp;  18&nbsp;# Above 5 lines of "message" prefaced by a tab, not spaces.  19&nbsp;# Spaces not affected by   &#60;&#60;-  .  20&nbsp;  21&nbsp;# Note that this option has no effect on *embedded* tabs.  22&nbsp;  23&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="HEREPASSP"></A></P><P>A <ICLASS="FIRSTTERM">here document</I> supports parameter and	command substitution.  It is therefore possible to pass different	parameters to the body of the here document, changing its output	accordingly.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX71B"></A><P><B>Example 18-5. Here document with parameter substitution</B></P><TABLE

⌨️ 快捷键说明

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