如何`猫<<在bash EOF`工作? [英] How does ` cat << EOF` work in bash?

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

问题描述

我需要写一个脚本来输入多行输入到一个程序(的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?

我主要指的是猫&LT;&LT; EOF ,我知道&GT; 输出到文件,&GT;&GT; 追加一个文件,&LT; 从文件中读取输入。

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

这是什么&LT;&LT;?究竟

和有一个手册页呢?

推荐答案

这就是所谓的定界符格式的提供一个字符串转换成标准输入。见<一href=\"https://en.wikipedia.org/wiki/Here_document#Unix_shells\">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.

男人庆典

这类型的重定向指示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

没有参数扩展,命令替换,算术扩展,或
  执行的路径扩展
  。如果字符的任何字符
  报价时,
  分隔符是引用删除对,两条线的结果
  在这里的文档中不展开。
  如果是不带引号的,所有的行
  此文档受到参数扩展,命令
  替换和算术
  扩张。在后一种情况下,该
  字符序列 \\&LT;&换行符GT;
  忽略,而 \\ 必须使用引用字符 \\ $ 和`。

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

如果重定向操作符是&LT;&LT; - ,那么所有领先的制表符
  从输入线和被剥离
  包含行分隔符。这个
  允许这里的文档shell脚本中要以自然的方式缩进。

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.

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

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