-1

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

uberrebu
  • 3,597
  • 9
  • 38
  • 73
  • no not same..that question has no indentation...that one is easy...when the content has indentations as shown in my question is a problem – uberrebu Apr 16 '19 at 02:31
  • 1
    Different question, same answers. Use tabs, not spaces, and don't indent your closing string. – miken32 Apr 16 '19 at 02:34
  • i used tabs already..as you can see i added the `>>-EOF` because they mentioned using tabs for the spaces.i did that and no did not work...i have spent way too many hours on this thing and it is annoying that am spending hours on something i have no logical reason why it should not just work – uberrebu Apr 16 '19 at 02:36
  • you can paste your answer you believe will work and i can try it and see..i posted all the code above in the question..if you feel confident your answer will work post it and i will try it..thanks for trying to help – uberrebu Apr 16 '19 at 02:37
  • I just gave you two reasons why it won't work. "Use tabs, not spaces, and don't indent your closing string." I'm not going to answer the question because I believe it should be closed. – miken32 Apr 16 '19 at 02:37
  • i used tabs..not sure if the question does not show as tabs but i used tabs and what you mean by closing string? – uberrebu Apr 16 '19 at 02:37
  • The EOF that terminates the heredoc. And the question code makes it looks like it has spaces, sorry to misunderstand that. – miken32 Apr 16 '19 at 02:38
  • this is part of cloudformation template..i posted the userdata part which is where am applying the `cat`..so maybe paste what you think i can try in the answer section? and i can try it – uberrebu Apr 16 '19 at 02:39
  • you plan posting an answer? – uberrebu Apr 16 '19 at 02:47
  • @miken32 i keep telling you there is no question that is same as my question..if you feel so then paste an answer to try and i will tell you if it solves the issue..please remove the header because that is false statement – uberrebu Apr 16 '19 at 04:01
  • But @miken32 already posted the answer! "don't indent your closing string... The `EOF` that terminates the heredoc". As one of the above mentioned answer https://stackoverflow.com/a/18660985/2886891says: "The `EOF` token must be at the beginning of the line, you can't indent it along with the block of code it goes with." – Honza Zidek Apr 16 '19 at 09:08
  • it did not work..i think i mentioned that already..again if you have an answer please post it below in this post and i will try it and if it works i will mark as answer...stop telling me there is an answer somewhere...the content of file is docker-compose and it needs to follow certain indentation as well – uberrebu Apr 16 '19 at 14:38
  • i tend to spend more time going back and forth with you mods rather than engaging with ppl trying to answer my question...the experience is just declining gradually every time now...sometimes i feel the mods do not even understand question but look at title and think questions are same..if you are technical enough to understand my question then you will take more steps back and again post your answer and lets see then – uberrebu Apr 16 '19 at 14:42
  • Yep. closing EOF will give an error if its indented. – mirageglobe Dec 15 '19 at 21:10

1 Answers1

-4

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.

Rocketman173
  • 101
  • 1
  • 1
  • i tried `>>EOF` already and many other things...so `>>EOF` did not work either – uberrebu Apr 16 '19 at 02:30
  • 3
    `<<-` is valid syntax. – miken32 Apr 16 '19 at 02:35
  • @uberrebu I was wrong about what I said (sorry), but have you tried removing all the space before the `EOF`? As in, moving it all the way to the left. It's worth a shot, I think. – Rocketman173 Apr 16 '19 at 02:36
  • that is a part of cloudformation template so yes i have tried putting the closing EOF all the way to start of that line and nopes did not work..gave errors with cloudformation – uberrebu Apr 16 '19 at 02:38