PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 29, 2022

[FIXED] How to process or excape variables inside of EOF to write file content?

 October 29, 2022     eof, nginx, shell     No comments   

Issue

This is how I create a file (nginx.conf) via shell. As there are $ characters in the filecontent I'm using EOF.

if [ $type == "nginx" ]; then
    cat > ${path}/nginx.conf <<'EOF'
server {
listen 3000;

location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
    try_files $uri $uri/ /index.html =404;
}

include /etc/nginx/extra-conf.d/*.conf;
}
EOF
fi

Now I have to use a dynamic port value, so instead of listen 3000 I need to use listen $port. But this won't work, as in the content there is also $uri, which should be handled as text, not as a variable.


Solution

Using only the delimiter itself, either all parameters are expanded or none. You'll have to allow expansion, but escape the dollar signs for $uri to inhibit their expansion.

if [ "$type" = "nginx" ]; then
    cat > "${path}/nginx.conf" <<EOF
server {
listen $port;

location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
    try_files \$uri \$uri/ /index.html = 404;
}

include /etc/nginx/extra-conf.d/*.conf;
}
EOF
fi

The here document behaves like a double-quoted string:

$ foo=bar
$ echo "$foo"
bar
$ echo "\$foo"
$foo


Answered By - chepner
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing