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

📄 here-docs.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 3 页
字号:
  29&nbsp;  30&nbsp;echo "Listing of "$directory":"; echo  31&nbsp;(printf "PERMISSIONS LINKS OWNER GROUP SIZE MONTH DAY HH:MM PROG-NAME\n" \  32&nbsp;; ls -l "$directory" | sed 1d) | column -t  33&nbsp;  34&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>Using a <AHREF="here-docs.html#CATSCRIPTREF">cat script</A> is an        alternate way of accomplishing this.</P><P>      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;DOC_REQUEST=70   2&nbsp;   3&nbsp;if [ "$1" = "-h"  -o "$1" = "--help" ]     # Request help.   4&nbsp;then                                       # Use a "cat script" . . .   5&nbsp;  cat &#60;&#60;DOCUMENTATIONXX   6&nbsp;List the statistics of a specified directory in tabular format.   7&nbsp;---------------------------------------------------------------   8&nbsp;The command line parameter gives the directory to be listed.   9&nbsp;If no directory specified or directory specified cannot be read,  10&nbsp;then list the current working directory.  11&nbsp;  12&nbsp;DOCUMENTATIONXX  13&nbsp;exit $DOC_REQUEST  14&nbsp;fi</PRE></TD></TR></TABLE>      </P><P>See also <AHREF="contributed-scripts.html#ISSPAMMER2">Example A-30</A> for one more excellent example        of a self-documenting script.</P><P><ANAME="HERETEMP"></A></P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Here documents create temporary files, but these	    files are deleted after opening and are not accessible to	    any other process.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>bash -c 'lsof -a -p $$ -d0' &#60;&#60; EOF</B></TT> <TTCLASS="PROMPT">&#62; </TT><TTCLASS="USERINPUT"><B>EOF</B></TT> <TTCLASS="COMPUTEROUTPUT">lsof    1213 bozo    0r   REG    3,5    0 30386 /tmp/t1213-0-sh (deleted)</TT> 	      </PRE></TD></TR></TABLE>	  </P></TD></TR></TABLE></DIV><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Some utilities will not work inside a	  <ICLASS="FIRSTTERM">here document</I>.</P></TD></TR></TABLE></DIV><P><ANAME="INDENTEDLS"></A></P><DIVCLASS="WARNING"><TABLECLASS="WARNING"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/warning.png"HSPACE="5"ALT="Warning"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The closing <ICLASS="FIRSTTERM">limit string</I>,	  on the final line of a here document, must start in the	  <SPANCLASS="emphasis"><ICLASS="EMPHASIS">first</I></SPAN> character position. There can	  be <SPANCLASS="emphasis"><ICLASS="EMPHASIS">no leading whitespace</I></SPAN>. Trailing	  whitespace after the limit string likewise causes unexpected	  behavior. The whitespace prevents the limit string from being	  recognized.</P><P>	 <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;echo "----------------------------------------------------------------------"   4&nbsp;   5&nbsp;cat &#60;&#60;LimitString   6&nbsp;echo "This is line 1 of the message inside the here document."   7&nbsp;echo "This is line 2 of the message inside the here document."   8&nbsp;echo "This is the final line of the message inside the here document."   9&nbsp;     LimitString  10&nbsp;#^^^^Indented limit string. Error! This script will not behave as expected.  11&nbsp;  12&nbsp;echo "----------------------------------------------------------------------"  13&nbsp;  14&nbsp;#  These comments are outside the 'here document',  15&nbsp;#+ and should not echo.  16&nbsp;  17&nbsp;echo "Outside the here document."  18&nbsp;  19&nbsp;exit 0  20&nbsp;  21&nbsp;echo "This line had better not echo."  # Follows an 'exit' command.</PRE></TD></TR></TABLE>	 </P></TD></TR></TABLE></DIV><P>For those tasks too complex for a <SPANCLASS="QUOTE">"here	  document"</SPAN>, consider using the <BCLASS="COMMAND">expect</B>	  scripting language, which is specifically tailored for feeding	  input into interactive programs.</P><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="AEN16438"></A>18.1. Here Strings</H1><P><ANAME="HERESTRINGSREF"></A></P><P>A <ICLASS="FIRSTTERM">here string</I> can be considered as	   a stripped-down form of a <ICLASS="FIRSTTERM">here document</I>.	   It consists of nothing more than <BCLASS="COMMAND">COMMAND	   &#60;&#60;&#60;$WORD</B>, where <TTCLASS="VARNAME">$WORD</TT>	   is expanded and fed to the <TTCLASS="FILENAME">stdin</TT> of	   <TTCLASS="PARAMETER"><I>COMMAND</I></TT>.</P><P>As a simple example, consider this alternative to the <AHREF="internal.html#ECHOGREPREF">echo-grep</A> construction.</P><P>	  <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;# Instead of:   2&nbsp;if echo "$VAR" | grep -q txt   # if [[ $VAR = *txt* ]]   3&nbsp;# etc.   4&nbsp;   5&nbsp;# Try:   6&nbsp;if grep -q "txt" &#60;&#60;&#60; "$VAR"   7&nbsp;then   8&nbsp;   echo "$VAR contains the substring sequence \"txt\""   9&nbsp;fi  10&nbsp;# Thank you, Sebastian Kaminski, for the suggestion.</PRE></TD></TR></TABLE>        </P><P><ANAME="HSREAD"></A></P><P>Or, in combination with <AHREF="internal.html#READREF">read</A>:</P><P>	<TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;String="This is a string of words."   2&nbsp;   3&nbsp;read -r -a Words &#60;&#60;&#60; "$String"   4&nbsp;#  The -a option to "read"   5&nbsp;#+ assigns the resulting values to successive members of an array.   6&nbsp;   7&nbsp;echo "First word in String is:    ${Words[0]}"   # This   8&nbsp;echo "Second word in String is:   ${Words[1]}"   # is   9&nbsp;echo "Third word in String is:    ${Words[2]}"   # a  10&nbsp;echo "Fourth word in String is:   ${Words[3]}"   # string  11&nbsp;echo "Fifth word in String is:    ${Words[4]}"   # of  12&nbsp;echo "Sixth word in String is:    ${Words[5]}"   # words.  13&nbsp;echo "Seventh word in String is:  ${Words[6]}"   # (null)  14&nbsp;                                                 # Past end of $String.  15&nbsp;  16&nbsp;# Thank you, Francisco Lobo, for the suggestion.</PRE></TD></TR></TABLE>        </P><P><ANAME="HSPRE"></A></P><DIVCLASS="EXAMPLE"><HR><ANAME="PREPENDEX"></A><P><B>Example 18-13. Prepending a line to a file</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# prepend.sh: Add text at beginning of file.   3&nbsp;#   4&nbsp;#  Example contributed by Kenny Stauffer,   5&nbsp;#+ and slightly modified by document author.   6&nbsp;   7&nbsp;   8&nbsp;E_NOSUCHFILE=65   9&nbsp;  10&nbsp;read -p "File: " file   # -p arg to 'read' displays prompt.  11&nbsp;if [ ! -e "$file" ]  12&nbsp;then   # Bail out if no such file.  13&nbsp;  echo "File $file not found."  14&nbsp;  exit $E_NOSUCHFILE  15&nbsp;fi  16&nbsp;  17&nbsp;read -p "Title: " title  18&nbsp;cat - $file &#60;&#60;&#60;$title &#62; $file.new  19&nbsp;  20&nbsp;echo "Modified file is $file.new"  21&nbsp;  22&nbsp;exit 0  23&nbsp;  24&nbsp;# from 'man bash':  25&nbsp;# Here Strings  26&nbsp;# 	A variant of here documents, the format is:  27&nbsp;#   28&nbsp;# 		&#60;&#60;&#60;word  29&nbsp;#   30&nbsp;# 	The word is expanded and supplied to the command on its standard input.</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="MAILBOXGREP"></A><P><B>Example 18-14. Parsing a mailbox</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;#  Script by Francisco Lobo,   3&nbsp;#+ and slightly modified and commented by ABS Guide author.   4&nbsp;#  Used in ABS Guide with permission. (Thank you!)   5&nbsp;   6&nbsp;# This script will not run under Bash versions &#60; 3.0.   7&nbsp;   8&nbsp;   9&nbsp;E_MISSING_ARG=67  10&nbsp;if [ -z "$1" ]  11&nbsp;then  12&nbsp;  echo "Usage: $0 mailbox-file"  13&nbsp;  exit $E_MISSING_ARG  14&nbsp;fi  15&nbsp;  16&nbsp;mbox_grep()  # Parse mailbox file.  17&nbsp;{  18&nbsp;    declare -i body=0 match=0  19&nbsp;    declare -a date sender  20&nbsp;    declare mail header value  21&nbsp;  22&nbsp;  23&nbsp;    while IFS= read -r mail  24&nbsp;#         ^^^^                 Reset $IFS.  25&nbsp;#  Otherwise "read" will strip leading &#38; trailing space from its input.  26&nbsp;  27&nbsp;   do  28&nbsp;       if [[ $mail =~ "^From " ]]   # Match "From" field in message.  29&nbsp;       then  30&nbsp;          (( body  = 0 ))           # "Zero out" variables.  31&nbsp;          (( match = 0 ))  32&nbsp;          unset date  33&nbsp;  34&nbsp;       elif (( body ))  35&nbsp;       then  36&nbsp;            (( match ))  37&nbsp;            # echo "$mail"  38&nbsp;            # Uncomment above line if you want entire body of message to display.  39&nbsp;  40&nbsp;       elif [[ $mail ]]; then  41&nbsp;          IFS=: read -r header value &#60;&#60;&#60; "$mail"  42&nbsp;          #                          ^^^  "here string"  43&nbsp;  44&nbsp;          case "$header" in  45&nbsp;          [Ff][Rr][Oo][Mm] ) [[ $value =~ "$2" ]] &#38;&#38; (( match++ )) ;;  46&nbsp;          # Match "From" line.  47&nbsp;          [Dd][Aa][Tt][Ee] ) read -r -a date &#60;&#60;&#60; "$value" ;;  48&nbsp;          #                                  ^^^  49&nbsp;          # Match "Date" line.  50&nbsp;          [Rr][Ee][Cc][Ee][Ii][Vv][Ee][Dd] ) read -r -a sender &#60;&#60;&#60; "$value" ;;  51&nbsp;          #                                                    ^^^  52&nbsp;          # Match IP Address (may be spoofed).  53&nbsp;          esac  54&nbsp;  55&nbsp;       else  56&nbsp;          (( body++ ))  57&nbsp;          (( match  )) &#38;&#38;  58&nbsp;          echo "MESSAGE ${date:+of: ${date[*]} }"  59&nbsp;       #    Entire $date array             ^  60&nbsp;          echo "IP address of sender: ${sender[1]}"  61&nbsp;       #    Second field of "Received" line    ^  62&nbsp;  63&nbsp;       fi  64&nbsp;  65&nbsp;  66&nbsp;    done &#60; "$1" # Redirect stdout of file into loop.  67&nbsp;}  68&nbsp;  69&nbsp;  70&nbsp;mbox_grep "$1"  # Send mailbox file to function.  71&nbsp;  72&nbsp;exit $?  73&nbsp;  74&nbsp;# Exercises:  75&nbsp;# ---------  76&nbsp;# 1) Break the single function, above, into multiple functions,  77&nbsp;#+   for the sake of readability.  78&nbsp;# 2) Add additional parsing to the script, checking for various keywords.  79&nbsp;  80&nbsp;  81&nbsp;  82&nbsp;$ mailbox_grep.sh scam_mail  83&nbsp;  MESSAGE of Thu, 5 Jan 2006 08:00:56 -0500 (EST)   84&nbsp;  IP address of sender: 196.3.62.4</PRE></TD></TR></TABLE><HR></DIV><P>Exercise: Find other uses for <ICLASS="FIRSTTERM">here            strings</I>.</P></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="globbingref.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="io-redirection.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Globbing</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part5.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">I/O Redirection</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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