以下命令的作用是什么?
cat >file.c << EOF
C line
EOF
这会创建一个名为“ file.c”的空C文件,然后下面的行将是其中的内容吗?
答案 0 :(得分:0)
此shell构造的技术术语是此处文档(“此处”与“脚本中的数据在此处”相同)。如果只有cat >file.c
,则将读取标准输入,直到满足文件结束条件(在Unix上通常为CTRL-D)。结果将被写入file.c
。
<< EOF
部分现在指示Shell将stdin上的以下行传递到cat
(而不是将stdin连接到终端)。外壳程序将一行上的EOF
本身识别为结束标记。详细信息在您的Shell手册中,例如对于bash,它是此段:
Here Documents
This type of redirection instructs the shell to read input from the current source
until a line containing only word (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 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. In the latter
case, the character sequence \<newline> is ignored, and \ must be used to quote the
characters \, $, and ‘.
If the redirection operator is <<-, then all leading tab characters are stripped from
input lines and the line containing delimiter. This allows here-documents within
shell scripts to be indented in a natural fashion.