【问题标题】:Bash EOF block in EOF block possible?EOF 块中的 Bash EOF 块可能吗?
【发布时间】:2020-12-15 01:57:55
【问题描述】:

我正在尝试在文件中写入一个包含其自己的 EOF 块的块。这在 bash 中可行吗?

cat > terragrunt.hcl <<'EOF' 
# Automatically generated from script $(basename $0)

# Generate the AWS provider block
generate "provider" {
  path      = "provider.tf"
  if_exists = "overwrite_terragrunt"
  contents  = <<EOF
# Terragrunt root config
provider "aws" {
  # Only these AWS Account IDs may be operated on by this template
  allowed_account_ids = ["${local.account_id}"]
  region = "${local.infra_region}"
  version = "${local.customer_vars.locals.aws_provider_version}"
}
EOF
}

# Automatically generated from script $(basename $0)
EOF

正如预期的那样,它失败了:

❯ ./example.sh 
./example.sh: line 17: syntax error near unexpected token `}'
./example.sh: line 17: `}'

最好的解决办法是什么?

【问题讨论】:

    标签: bash eof heredoc


    【解决方案1】:

    您所说的“EOF 块”实际上称为"Here Document"(通常缩写为“heredoc”),字符串“EOF”实际上可以是您喜欢的任何“单词”。

    heredoc 包含匹配分隔符之前的所有内容,因此嵌套两个 heredocs 只是选择两个不同分隔符的情况:

    cat > terragrunt.hcl <<'ENDTERRAGRUNT' 
    # Automatically generated from script $(basename $0)
    
    # Generate the AWS provider block
    generate "provider" {
      path      = "provider.tf"
      if_exists = "overwrite_terragrunt"
      contents  = <<ENDROOTCONFIG
    # Terragrunt root config
    provider "aws" {
      # Only these AWS Account IDs may be operated on by this template
      allowed_account_ids = ["${local.account_id}"]
      region = "${local.infra_region}"
      version = "${local.customer_vars.locals.aws_provider_version}"
    }
    ENDROOTCONFIG
    }
    
    # Automatically generated from script $(basename $0)
    ENDTERRAGRUNT
    

    【讨论】:

      猜你喜欢
      • 2020-08-22
      • 2020-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      • 2019-02-23
      • 1970-01-01
      • 2015-10-20
      相关资源
      最近更新 更多