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

📄 here-docs.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 3 页
字号:
BORDER="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><ANAME="HEREPARAMSUB"></A></P><P>This is a useful script containing a here document with        parameter substitution.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX72"></A><P><B>Example 18-6. Upload a file pair to <ICLASS="FIRSTTERM">Sunsite</I> 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><ANAME="HEREESC"></A></P><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 18-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  16&nbsp;#+  the same effect.  17&nbsp;#   cat &#60;&#60;"Endofmessage"  18&nbsp;#   cat &#60;&#60;\Endofmessage  19&nbsp;  20&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="HERELIT"></A></P><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><P><B>Example 18-8. A script that generates another script</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# generate-script.sh   3&nbsp;# Based on an idea by Albert Reiner.   4&nbsp;   5&nbsp;OUTFILE=generated.sh         # Name of the file to generate.   6&nbsp;   7&nbsp;   8&nbsp;# -----------------------------------------------------------   9&nbsp;# 'Here document containing the body of the generated script.  10&nbsp;(  11&nbsp;cat &#60;&#60;'EOF'  12&nbsp;#!/bin/bash  13&nbsp;  14&nbsp;echo "This is a generated shell script."  15&nbsp;#  Note that since we are inside a subshell,  16&nbsp;#+ we can't access variables in the "outside" script.  17&nbsp;  18&nbsp;echo "Generated file will be named: $OUTFILE"  19&nbsp;#  Above line will not work as normally expected  20&nbsp;#+ because parameter expansion has been disabled.  21&nbsp;#  Instead, the result is literal output.  22&nbsp;  23&nbsp;a=7  24&nbsp;b=3  25&nbsp;  26&nbsp;let "c = $a * $b"  27&nbsp;echo "c = $c"  28&nbsp;  29&nbsp;exit 0  30&nbsp;EOF  31&nbsp;) &#62; $OUTFILE  32&nbsp;# -----------------------------------------------------------  33&nbsp;  34&nbsp;#  Quoting the 'limit string' prevents variable expansion  35&nbsp;#+ within the body of the above 'here document.'  36&nbsp;#  This permits outputting literal strings in the output file.  37&nbsp;  38&nbsp;if [ -f "$OUTFILE" ]  39&nbsp;then  40&nbsp;  chmod 755 $OUTFILE  41&nbsp;  # Make the generated file executable.  42&nbsp;else  43&nbsp;  echo "Problem in creating file: \"$OUTFILE\""  44&nbsp;fi  45&nbsp;  46&nbsp;#  This method can also be used for generating  47&nbsp;#+ C programs, Perl programs, Python programs, Makefiles,  48&nbsp;#+ and the like.  49&nbsp;  50&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="HERECS"></A></P><P>        It is possible to set a variable from the output of a here document.	This is actually a devious form of <AHREF="commandsub.html#COMMANDSUBREF">command substitution</A>.	<TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;variable=$(cat &#60;&#60;SETVAR   2&nbsp;This variable   3&nbsp;runs over multiple lines.   4&nbsp;SETVAR)   5&nbsp;   6&nbsp;echo "$variable"</PRE></TD></TR></TABLE>      </P><P><ANAME="HEREFUNC"></A></P><P>A here document can supply input to a function in the same        script.</P><DIVCLASS="EXAMPLE"><HR><ANAME="HF"></A><P><B>Example 18-9. Here documents and functions</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# here-function.sh   3&nbsp;   4&nbsp;GetPersonalData ()   5&nbsp;{   6&nbsp;  read firstname   7&nbsp;  read lastname   8&nbsp;  read address   9&nbsp;  read city   10&nbsp;  read state   11&nbsp;  read zipcode  12&nbsp;} # This certainly looks like an interactive function, but...  13&nbsp;  14&nbsp;  15&nbsp;# Supply input to the above function.  16&nbsp;GetPersonalData &#60;&#60;RECORD001  17&nbsp;Bozo  18&nbsp;Bozeman  19&nbsp;2726 Nondescript Dr.  20&nbsp;Baltimore  21&nbsp;MD  22&nbsp;21226  23&nbsp;RECORD001  24&nbsp;  25&nbsp;  26&nbsp;echo  27&nbsp;echo "$firstname $lastname"  28&nbsp;echo "$address"  29&nbsp;echo "$city, $state $zipcode"  30&nbsp;echo  31&nbsp;  32&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="ANONHEREDOC0"></A></P><P>It is possible to use <SPANCLASS="TOKEN">:</SPAN> as a dummy command        accepting output from a here document. This, in effect, creates an	<SPANCLASS="QUOTE">"anonymous"</SPAN> here document.</P><DIVCLASS="EXAMPLE"><HR><ANAME="ANONHEREDOC"></A><P><B>Example 18-10. <SPANCLASS="QUOTE">"Anonymous"</SPAN> Here Document</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;: &#60;&#60;TESTVARIABLES   4&nbsp;${HOSTNAME?}${USER?}${MAIL?}  # Print error message if one of the variables not set.   5&nbsp;TESTVARIABLES   6&nbsp;   7&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="CBLOCK1"></A></P><DIVCLASS="TIP"><TABLECLASS="TIP"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/tip.png"HSPACE="5"ALT="Tip"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>A variation of the above technique permits <SPANCLASS="QUOTE">"commenting        out"</SPAN> blocks of code.</P></TD></TR></TABLE></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="COMMENTBLOCK"></A><P><B>Example 18-11. Commenting out a block of code</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# commentblock.sh   3&nbsp;   4&nbsp;: &#60;&#60;COMMENTBLOCK   5&nbsp;echo "This line will not echo."   6&nbsp;This is a comment line missing the "#" prefix.   7&nbsp;This is another comment line missing the "#" prefix.   8&nbsp;   9&nbsp;&#38;*@!!++=  10&nbsp;The above line will cause no error message,  11&nbsp;because the Bash interpreter will ignore it.  12&nbsp;COMMENTBLOCK  13&nbsp;  14&nbsp;echo "Exit value of above \"COMMENTBLOCK\" is $?."   # 0  15&nbsp;# No error shown.  16&nbsp;echo  17&nbsp;  18&nbsp;  19&nbsp;#  The above technique also comes in useful for commenting out  20&nbsp;#+ a block of working code for debugging purposes.  21&nbsp;#  This saves having to put a "#" at the beginning of each line,  22&nbsp;#+ then having to go back and delete each "#" later.  23&nbsp;  24&nbsp;echo "Just before commented-out code block."  25&nbsp;#  The lines of code between the double-dashed lines will not execute.  26&nbsp;#  ===================================================================  27&nbsp;: &#60;&#60;DEBUGXXX  28&nbsp;for file in *  29&nbsp;do  30&nbsp; cat "$file"  31&nbsp;done  32&nbsp;DEBUGXXX  33&nbsp;#  ===================================================================  34&nbsp;echo "Just after commented-out code block."  35&nbsp;  36&nbsp;exit 0  37&nbsp;  38&nbsp;  39&nbsp;  40&nbsp;######################################################################  41&nbsp;#  Note, however, that if a bracketed variable is contained within  42&nbsp;#+ the commented-out code block,  43&nbsp;#+ then this could cause problems.  44&nbsp;#  for example:  45&nbsp;  46&nbsp;  47&nbsp;#/!/bin/bash  48&nbsp;  49&nbsp;  : &#60;&#60;COMMENTBLOCK  50&nbsp;  echo "This line will not echo."  51&nbsp;  &#38;*@!!++=  52&nbsp;  ${foo_bar_bazz?}  53&nbsp;  $(rm -rf /tmp/foobar/)  54&nbsp;  $(touch my_build_directory/cups/Makefile)  55&nbsp;COMMENTBLOCK  56&nbsp;  57&nbsp;  58&nbsp;$ sh commented-bad.sh  59&nbsp;commented-bad.sh: line 3: foo_bar_bazz: parameter null or not set  60&nbsp;  61&nbsp;# The remedy for this is to strong-quote the 'COMMENTBLOCK' in line 49, above.  62&nbsp;  63&nbsp;  : &#60;&#60;'COMMENTBLOCK'  64&nbsp;  65&nbsp;# Thank you, Kurt Pfeifle, for pointing this out.</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="HSELFDOC"></A></P><DIVCLASS="TIP"><TABLECLASS="TIP"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/tip.png"HSPACE="5"ALT="Tip"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Yet another twist of this nifty trick makes        <SPANCLASS="QUOTE">"self-documenting"</SPAN> scripts possible.</P></TD></TR></TABLE></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="SELFDOCUMENT"></A><P><B>Example 18-12. A self-documenting script</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# self-document.sh: self-documenting script   3&nbsp;# Modification of "colm.sh".   4&nbsp;   5&nbsp;DOC_REQUEST=70   6&nbsp;   7&nbsp;if [ "$1" = "-h"  -o "$1" = "--help" ]     # Request help.   8&nbsp;then   9&nbsp;  echo; echo "Usage: $0 [directory-name]"; echo  10&nbsp;  sed --silent -e '/DOCUMENTATIONXX$/,/^DOCUMENTATIONXX$/p' "$0" |  11&nbsp;  sed -e '/DOCUMENTATIONXX$/d'; exit $DOC_REQUEST; fi  12&nbsp;  13&nbsp;  14&nbsp;: &#60;&#60;DOCUMENTATIONXX  15&nbsp;List the statistics of a specified directory in tabular format.  16&nbsp;---------------------------------------------------------------  17&nbsp;The command line parameter gives the directory to be listed.  18&nbsp;If no directory specified or directory specified cannot be read,  19&nbsp;then list the current working directory.  20&nbsp;  21&nbsp;DOCUMENTATIONXX  22&nbsp;  23&nbsp;if [ -z "$1" -o ! -r "$1" ]  24&nbsp;then  25&nbsp;  directory=.  26&nbsp;else  27&nbsp;  directory="$1"  28&nbsp;fi  

⌨️ 快捷键说明

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