【bash学習011】Here documentとは
5523 ワード
eg 1 here document:すぐにドキュメントを作成し、プロンプト情報を出力したり、いくつかの内容を自動的に入力したりすることができます.標準的な書式は基本的に次のスクリプトと同じです.
eg 2変数名も直ちにドキュメントで認識できます
eg 4隠蔽の誤り(「誤り」は裸の男で、彼は言葉を読んで「あなたは私が見えない、あなたは私が見えない、あなたは私が見えない、あなたは私が見えない」と言った.たまに隠すのはとてもいいですが、目の先が細かくさえあれば、うっかり漏れた馬の足を見つけることができます.
すべての例はhttp://tldp.org/LDP/abs/html/here-docs.html(ABS高度bashスクリプトガイド)から来ています.
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
" \
; ls -l "$directory" | sed 1d) | column -t
exit 0
すべての例はhttp://tldp.org/LDP/abs/html/here-docs.html(ABS高度bashスクリプトガイド)から来ています.