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

📄 here-docs.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!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.57"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Beyond the Basics"HREF="part3.html"><LINKREL="PREVIOUS"TITLE="Applications"HREF="redirapps.html"><LINKREL="NEXT"TITLE="Recess Time"HREF="recess-time.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"><TABLEWIDTH="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="redirapps.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="recess-time.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="HERE-DOCS">Chapter 17. Here Documents</A></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></I></TD></TR><TR><TDWIDTH="45%">&nbsp;</TD><TDWIDTH="45%"ALIGN="RIGHT"VALIGN="TOP"><I><SPANCLASS="ATTRIBUTION">Aldous Huxley, <SPANCLASS="QUOTE">"Island"</SPAN></SPAN></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>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="EMPHASIS">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="EMPHASIS">limit string</I> sufficiently	unusual that it will not occur anywhere in the command list and	confuse matters.</P><P>Note that <ICLASS="EMPHASIS">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 17-1. <BCLASS="COMMAND">broadcast</B>: 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>Even such unlikely candidates as <ICLASS="FIRSTTERM">vi</I> lend        themselves to <ICLASS="EMPHASIS">here documents</I>.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX69"></A><P><B>Example 17-2. <BCLASS="COMMAND">dummyfile</B>: 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>. <ICLASS="EMPHASIS">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 17-3. Multi-line message using <BCLASS="COMMAND">cat</B></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>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 17-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>A <ICLASS="EMPHASIS">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 17-5. Here document with parameter substitution</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# Another 'cat' here document, using parameter substitution.   3&nbsp;   4&nbsp;# Try it with no command line parameters,   ./scriptname   5&nbsp;# Try it with one command line parameter,   ./scriptname Mortimer   6&nbsp;# Try it with one two-word quoted command line parameter,   7&nbsp;#                           ./scriptname "Mortimer Jones"   8&nbsp;   9&nbsp;CMDLINEPARAM=1     #  Expect at least command line parameter.  10&nbsp;  11&nbsp;if [ $# -ge $CMDLINEPARAM ]  12&nbsp;then  13&nbsp;  NAME=$1          #  If more than one command line param,  14&nbsp;                   #+ then just take the first.  15&nbsp;else  16&nbsp;  NAME="John Doe"  #  Default, if no command line parameter.  17&nbsp;fi    18&nbsp;  19&nbsp;RESPONDENT="the author of this fine script"    20&nbsp;    21&nbsp;  22&nbsp;cat &#60;&#60;Endofmessage  23&nbsp;  24&nbsp;Hello, there, $NAME.  25&nbsp;Greetings to you, $NAME, from $RESPONDENT.  26&nbsp;  27&nbsp;# This comment shows up in the output (why?).  28&nbsp;  29&nbsp;Endofmessage  30&nbsp;  31&nbsp;# Note that the blank lines show up in the output.  32&nbsp;# So does the "comment".  33&nbsp;  34&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>This is a useful script containing a here document with        parameter substitution.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX72"></A><P><B>Example 17-6. Upload a file pair to <SPANCLASS="QUOTE">"Sunsite"</SPAN> incoming	  directory</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# upload.sh   3&nbsp;   4&nbsp;#  Upload file pair (Filename.lsm, Filename.tar.gz)   5&nbsp;#+ to incoming directory at Sunsite/UNC (ibiblio.org).   6&nbsp;#  Filename.tar.gz is the tarball itself.   7&nbsp;#  Filename.lsm is the descriptor file.   8&nbsp;#  Sunsite requires "lsm" file, otherwise will bounce contributions.   9&nbsp;  10&nbsp;  11&nbsp;E_ARGERROR=65  12&nbsp;  13&nbsp;if [ -z "$1" ]  14&nbsp;then  15&nbsp;  echo "Usage: `basename $0` Filename-to-upload"  16&nbsp;  exit $E_ARGERROR  17&nbsp;fi    18&nbsp;  19&nbsp;  20&nbsp;Filename=`basename $1`           # Strips pathname out of file name.  21&nbsp;  22&nbsp;Server="ibiblio.org"  23&nbsp;Directory="/incoming/Linux"  24&nbsp;#  These need not be hard-coded into script,  25&nbsp;#+ but may instead be changed to command line argument.  26&nbsp;  27&nbsp;Password="your.e-mail.address"   # Change above to suit.  28&nbsp;  29&nbsp;ftp -n $Server &#60;&#60;End-Of-Session  30&nbsp;# -n option disables auto-logon  31&nbsp;  32&nbsp;user anonymous "$Password"  33&nbsp;binary  34&nbsp;bell                             # Ring 'bell' after each file transfer.  35&nbsp;cd $Directory  36&nbsp;put "$Filename.lsm"  37&nbsp;put "$Filename.tar.gz"  38&nbsp;bye  39&nbsp;End-Of-Session  40&nbsp;  41&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>Quoting or escaping the <SPANCLASS="QUOTE">"limit string"</SPAN> at the        head of a here document disables parameter substitution within its	body.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX71C"></A><P><B>Example 17-7. Parameter substitution turned off</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;#  A 'cat' here document, but with parameter substitution disabled.   3&nbsp;   4&nbsp;NAME="John Doe"   5&nbsp;RESPONDENT="the author of this fine script"     6&nbsp;   7&nbsp;cat &#60;&#60;'Endofmessage'   8&nbsp;   9&nbsp;Hello, there, $NAME.  10&nbsp;Greetings to you, $NAME, from $RESPONDENT.  11&nbsp;  12&nbsp;Endofmessage  13&nbsp;  14&nbsp;#  No parameter substitution when the "limit string" is quoted or escaped.  15&nbsp;#  Either of the following at the head of the here document would have the same effect.  16&nbsp;#  cat &#60;&#60;"Endofmessage"  17&nbsp;#  cat &#60;&#60;\Endofmessage  18&nbsp;  19&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>Disabling parameter substitution permits outputting literal text.        Generating scripts or even program code is one use for this.</P><DIVCLASS="EXAMPLE"><HR><ANAME="GENERATESCRIPT"></A

⌨️ 快捷键说明

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