In this问题,在this问题中使用了这种BASH脚本语法:
#!/bin/bash
sql << EOF
**some SQL commands**
EOF
EOF
实际上可以是任何东西。它适用于我,但我很好奇为什么它的工作原理和方式。调用此模式的正确方法是什么。你能找到与这个功能相对应的Bash文档吗?
答案 0 :(得分:5)
% man bash | grep --max-count=1 '<<' -C 7
Here Documents
This type of redirection instructs the shell to read input from the current source until a line containing only delimiter (with no trailing blanks)
is seen. All of the lines read up to that point are then used as the standard input for a command.
The format of here-documents is:
<<[-]word
here-document
delimiter
No parameter and variable expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. If any characters in
word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted,
all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion, the character sequence \<new‐
line> is ignored, and \ must be used to quote the characters \, $, and `.
答案 1 :(得分:2)
您找到的构造称为&#34;此处为文档&#34;并在Bash手册的标题&#34; Here Documents&#34; (其次是&#34; Here Strings&#34;相关)。它是实现shell的POSIX标准的任何shell的标准功能。我相信&#34;这里是字符串&#34;是一个Bash扩展。
最好按照示例进行操作,并使用大写字符串标记here文档的结尾。另一个好建议是使用XXX_END
(XXX
为XML
,DOC
,DATA
,SQL
或其他任何内容#39;重新输入命令)作为分隔符,以进一步记录此处的文档。
如果引用用作分隔符的单词,shell将不对此处文档的内容进行参数扩展(等):
$ cat <<'TEST_END'
my $HOME is where I hang my hat
TEST_END
结果:
my $HOME is where I hang my hat
,而
$ cat <<TEST_END
my $HOME is where I hang my hat
TEST_END
(在我的笔记本电脑上)导致
my /Users/kk is where I hang my hat