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

查看:36
本文介绍了"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 格式 以将字符串提供到标准输入中.请参阅 https://en.wikipedia.org/wiki/Here_document#Unix_shells 了解更多详情.

This is called heredoc format to provide a string into stdin. See https://en.wikipedia.org/wiki/Here_document#Unix_shells for more details.

来自man bash:

这种类型的重定向指示 shell 从当前源直到一行只包含单词(没有尾随空白)被看到.

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.

此处文档的格式为:

          <<[-]word
                  here-document
          delimiter

无参数扩展、命令替换、算术扩展或路径名扩展在.如果 word 中的任何字符是引述,delimiter 是去除 word 上引用的结果,而行here-document 中的内容没有展开.如果 word 没有被引用,则所有行here-document 进行参数扩展,命令代入和算术扩张.在后一种情况下,字符序列 是忽略,并且必须使用 来引用字符 $`.

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.

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

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