Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

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!
0
Daniel Wilson
Asked:
Daniel Wilson
1 Solution
 
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
 
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now