What's wrong with the heredoc in this shell script? It drops the variables with $.

I'm scripting a server setup, including an Nginx configuration file.  This is the script command I use:
cat >./default.conf <<NGINXPROXY
server {
    location / {
        fastcgi_pass  localhost:3000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param QUERY_STRING    $query_string;
    }
}
NGINXPROXY

Open in new window


But the conf file doesn't get the entire thing.  When I cat the file, I get:
cat >./default.conf <<NGINXPROXY
server {
    location / {
        fastcgi_pass  localhost:3000;
        fastcgi_param SCRIPT_FILENAME   ; 
        fastcgi_param QUERY_STRING    ;
    }
}
NGINXPROXY

Open in new window


Can you tell what's wrong with my script?  Thanks!
LVL 32
Daniel WilsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
Use quotes around

$document_root$fastcgi_script_name

otherwise it will be interpreted.
0
simon3270Commented:
Alternatively, put a backslash in front of the NGINXPROXY on the first line

    cat >./default.conf <<\NGINXPROXY

That will stop $ being interpreted.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tel2Commented:
Hi Gary,

I can't get your solution to work in bash with 'single' or "double" quotes.  Check this out:

$ cat here.sh
#!/bin/bash

document_root=DR
fastcgi_script_name=FSN
query_string=QS

cat >./default.conf <<NGINXPROXY
server {
    location / {
        fastcgi_pass  localhost:3000;
        fastcgi_param SCRIPT_FILENAME "$document_root" '$fastcgi_script_name';
        fastcgi_param QUERY_STRING    $query_string;
    }
}
NGINXPROXY

$ ./here.sh

$ cat default.conf
server {
    location / {
        fastcgi_pass  localhost:3000;
        fastcgi_param SCRIPT_FILENAME "DR" 'FSN';
        fastcgi_param QUERY_STRING    QS;
    }
}

What shell are you using, Gary?  Does your solution work for you?  What am I doing wrong?

Simon's solution works OK for me.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

GaryCommented:
It was just a guess I never tested it but you have a working solution
0
Daniel WilsonAuthor Commented:
Thanks!  I couldn't get Gary's option to work either, but Simon's worked great.
0
simon3270Commented:
Mine was the simple "replace nothing" option.  If you have a here document with some $ variables that you want to replace with their shell variable  values, and some to be left alone, remove the \ before NGINXPROXY and add a \ before each of the $s that you want to be left alone.  Can get a bit messy!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.