【bash学习011】Here document是什么

eg 1 here document:立即文档,可以用于输出提示信息,或者自动输入某些内容,其标准书写格式基本与下面脚本中的一个模样;

  1 #!/bin/bash

  2 

  3 #  'echo'对于打印单行消息来说是非常好用的, 

  4 #+  但是在打印消息块时可能就有点问题了. 

  5 #   'cat' here document可以解决这个限制. 

  6 

  7 cat <<End-of-message

  8 -------------------------------------

  9 This is line 1 of the message.

 10 This is line 2 of the message.

 11 This is line 3 of the message.

 12 This is line 4 of the message.

 13 This is the last line of the message.

 14 -------------------------------------

 15 End-of-message

 16 

 17 #  用下边这行代替上边的第7行, 

 18 #+   cat > $Newfile <<End-of-message

 19 #+       ^^^^^^^^^^

 20 #+ 那么就会把输出写到文件$Newfile中, 而不是stdout. 

 21 

 22 exit 0

eg 2 变量名也能被立即文档识别

  

#!/bin/bash

# Another 'cat' here document, using parameter substitution.



# Try it with no command-line parameters,   ./scriptname

# Try it with one command-line parameter,   ./scriptname Mortimer

# Try it with one two-word quoted command-line parameter,

#                           ./scriptname "Mortimer Jones"



CMDLINEPARAM=1     #  Expect at least command-line parameter.



if [ $# -ge $CMDLINEPARAM ]

then

  NAME=$1          #  If more than one command-line param,

                   #+ then just take the first.

else

  NAME="John Doe"  #  Default, if no command-line parameter.

fi  



RESPONDENT="the author of this fine script"  

  



cat <<Endofmessage



Hello, there, $NAME.

Greetings to you, $NAME, from $RESPONDENT.



# This comment shows up in the output (why?).



Endofmessage



# Note that the blank lines show up in the output.

# So does the comment.



exit


eg 3 在当前脚本中创建另一个脚本(或c语言程序或者其他)
#!/bin/bash

# generate-script.sh

# Based on an idea by Albert Reiner.



OUTFILE=generated.sh         # Name of the file to generate.





# -----------------------------------------------------------

# 'Here document containing the body of the generated script.

(

cat <<'EOF'

#!/bin/bash



echo "This is a generated shell script."

#  Note that since we are inside a subshell,

#+ we can't access variables in the "outside" script.



echo "Generated file will be named: $OUTFILE"

#  Above line will not work as normally expected

#+ because parameter expansion has been disabled.

#  Instead, the result is literal output.



a=7

b=3



let "c = $a * $b"

echo "c = $c"



exit 0

EOF

) > $OUTFILE

# -----------------------------------------------------------



#  Quoting the 'limit string' prevents variable expansion

#+ within the body of the above 'here document.'

#  This permits outputting literal strings in the output file.



if [ -f "$OUTFILE" ]

then

  chmod 755 $OUTFILE

  # Make the generated file executable.

else

  echo "Problem in creating file: \"$OUTFILE\""

fi



#  This method can also be used for generating

#+ C programs, Perl programs, Python programs, Makefiles,

#+ and the like.



exit 0


variable=$(cat <<SETVAR

This variable

runs over multiple lines.

SETVAR)



echo "$variable"

eg 4 隐蔽的错误(“错误”是一个裸男,他正念念有词地说:“你看不见我,你看不见我,你就是看不见我。”虽然它偶尔可以隐藏地非常好,但只要眼尖心细,你还是能发现它不小心漏出的马脚。)

#!/bin/bash



echo "----------------------------------------------------------------------"



cat <<LimitString

echo "This is line 1 of the message inside the here document."

echo "This is line 2 of the message inside the here document."

echo "This is the final line of the message inside the here document."

     LimitString(错误在这里,LimitString前面不能有空格)

#^^^^Indented limit string. Error! This script will not behave as expected.



echo "----------------------------------------------------------------------"



#  These comments are outside the 'here document',

#+ and should not echo.



echo "Outside the here document."



exit 0



echo "This line had better not echo."  # Follows an 'exit' command.

eg 5 我也看不懂,留在这里,慢慢地看
#!/bin/bash

# self-document.sh: self-documenting script

# Modification of "colm.sh".



DOC_REQUEST=70



if [ "$1" = "-h"  -o "$1" = "--help" ]     # Request help.

then

  echo; echo "Usage: $0 [directory-name]"; echo

  sed --silent -e '/DOCUMENTATIONXX$/,/^DOCUMENTATIONXX$/p' "$0" |

  sed -e '/DOCUMENTATIONXX$/d'; exit $DOC_REQUEST; fi





: <<DOCUMENTATIONXX

List the statistics of a specified directory in tabular format.

---------------------------------------------------------------

The command-line parameter gives the directory to be listed.

If no directory specified or directory specified cannot be read,

then list the current working directory.



DOCUMENTATIONXX



if [ -z "$1" -o ! -r "$1" ]

then

  directory=.

else

  directory="$1"

fi  



echo "Listing of "$directory":"; echo

(printf "PERMISSIONS LINKS OWNER GROUP SIZE MONTH DAY HH:MM PROG-NAME\n" \

; ls -l "$directory" | sed 1d) | column -t

exit 0

  所有用例均来自http://tldp.org/LDP/abs/html/here-docs.html(ABS高级bash脚本指南)。

你可能感兴趣的:(document)