cat EOF issues with indentation [duplicate]

2019-08-28 23:06发布

问题:

This question already has an answer here:

  • here-document gives 'unexpected end of file' error 4 answers
  • Multi-line string with extra space (preserved indentation) 7 answers

NOTE: If anyone feel this question has been answered before please paste your answer below otherwise please don't interfere with people trying to help someone who needs help. Thanks

I am not sure why it is such an hassle trying to cat content to file when the content is indented

Can anyone point to how to close this heredoc block

UserData:
  "Fn::Base64":
    !Sub |
      #!/bin/bash -xe
      NONCE=$(cat /tmp/nonce)
      cat <<-EOF > /tmp/docker-compose.yaml
      version: '3.5'
      services:
      onboarding:
          container_name: nginx
          image: nginx:${BuildId}
          restart: always
            ports:
            - 80:80
          environment:
            ENV: ${Env}
            AWS_REGION: ${Region}
            NONCE: $NONCE
      EOF
      source ~/.bashrc

But issue is cat is not closed properly with the closing "EOF".

Content of the file /tmp/docker-compose.yaml

version: '3.5'
services:
onboarding:
    container_name: nginx
    image: nginx:${BuildId}
    restart: always
      ports:
      - 80:80
    environment:
      ENV: ${Env}
      AWS_REGION: ${Region}
      NONCE: $NONCE
EOF
source ~/.bashrc

but this is what am expecting to see

version: '3.5'
services:
onboarding:
    container_name: nginx
    image: nginx:${BuildId}
    restart: always
      ports:
      - 80:80
    environment:
      ENV: ${Env}
      AWS_REGION: ${Region}
      NONCE: $NONCE

First why is this heredoc not so simple even with indentations? I know it works fine when there are no spaces at all in the content but man not sure why it is such a hassle when content has spaces

Any better alternative as am about to give up on cat because of this problem

Any help will be appreciated Thanks

回答1:

You have written <<-EOF, when I think you meant to write <<EOF.

Bash isn't closing your heredoc because it expects -EOF to close it, because of how you opened the heredoc. Either change the ending from EOF to -EOF, or, and this is what I'd suggest, change <<-EOF to <<EOF.