" cat<< EOF"在bash上工作? [英] How does "cat << EOF" work in bash?

查看:84
本文介绍了" cat<< EOF"在bash上工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写脚本以将多行输入输入到程序(psql).

I needed to write a script to enter multi-line input to a program (psql).

经过一番谷歌搜索,我发现以下语法有效:

After a bit of googling, I found the following syntax works:

cat << EOF | psql ---params
BEGIN;

`pg_dump ----something`

update table .... statement ...;

END;
EOF

这将正确构造多行字符串(从BEGIN;END;,包括端值),并将其作为输入输入到psql.

This correctly constructs the multi-line string (from BEGIN; to END;, inclusive) and pipes it as an input to psql.

但是我不知道它是如何/为什么工作的,请问有人可以解释一下吗?

But I have no idea how/why it works, can some one please explain?

我主要指的是cat << EOF,我知道>输出到文件,>>附加到文件,<从文件读取输入.

I'm referring mainly to cat << EOF, I know > outputs to a file, >> appends to a file, < reads input from file.

<<到底能做什么?

有手册页吗?

推荐答案

这称为 heredoc 格式,用于在stdin中提供字符串.有关更多详细信息,请参见 https://en.wikipedia.org/wiki/Here_document#Unix_shells

来自man 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.

here-documents的格式为:

The format of here-documents is:

          <<[-]word
                  here-document
          delimiter

无参数扩展,命令替换,算术扩展或 路径名扩展在 单词.如果单词中的任何字符 引用, 定界符单词和行中引号被删除的结果 此处文档中的内容未展开. 如果单词未加引号,则 此处文档要进行参数扩展,命令 替代和算术 扩张.在后一种情况下, 字符序列\<newline>是 忽略,并且必须使用\引用字符\$`.

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 `.

如果重定向操作符为<<-,则所有前导制表符 从输入线剥去, 包含定界符的行.这 允许以自然的方式缩进shell脚本中的here-document.

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.

这篇关于&quot; cat&lt;&lt; EOF"在bash上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆