运行 SQL 时 Bash 中的 `<<-EOSQL` 代码块是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38800277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:00:27  来源:igfitidea点击:

What is the `<<-EOSQL` code block in Bash when running SQL?

bashpostgresqldockerenvironment-variables

提问by modulitos

I need to execute a bash script containing SQL, so I am using a script to add custom configurations to a Postgres Docker container, according to the docs here:

根据此处的文档,我需要执行一个包含 SQL 的 bash 脚本,因此我使用脚本将自定义配置添加到 Postgres Docker 容器:

https://github.com/docker-library/docs/tree/master/postgres#how-to-extend-this-image

https://github.com/docker-library/docs/tree/master/postgres#how-to-extend-this-image

But I don't know what EOSQLmeans. Here is an example of my script taken from the docs above:

但不知道是什么EOSQL意思。这是我从上面的文档中获取的脚本示例:

#!/bin/bash
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
    CREATE USER docker;
    CREATE DATABASE docker;
    GRANT ALL PRIVILEGES ON DATABASE docker TO docker;
    CREATE EXTENSION $MY_EXTENSION;
EOSQL

So, what is EOSQL? I cannot seem to find much information about this command or keyword.

那么,什么是 EOSQL?我似乎找不到有关此命令或关键字的太多信息。

回答by Matt

EOSQLis a limit string for a Bash Here Documentblock. The limit string can be any text that doesn't appear in your block. It signifies the start and end of the text block.

EOSQLBash Here Document块的限制字符串。限制字符串可以是未出现在您的块中的任何文本。它表示文本块的开始和结束。

Variable substitution will work as normal in a here document:

变量替换将在此处文档中正常工作:

#!/usr/bin/env bash
cat <<-EOF
$MY_EXTENSION
EOF

Then running that with the variable set:

然后使用变量集运行它:

$ MY_EXTENSION=something ./test.sh
something

In Docker you will need ENV MY_EXTENSION=somethingin your Dockerfileor docker run -e MY_EXTENSION=something <image>on your command line for the environment to be setup.

在 Docker 中,您需要ENV MY_EXTENSION=something在命令行中Dockerfiledocker run -e MY_EXTENSION=something <image>命令行中设置环境。