Pass variable from shell script to config file

Data Mastery :

I have to write a shell script to install multiple services dynamically. I don´t know too much about shell scripting or the unix shell general, so I really need some help. This is my shell file.

#!/bin/bash
# Ask the user for their name
echo What is the name of your domain?
read varname

echo You passed the domain name to your domain $varname successfully

This is my nginx.conf file.

server {
  listen                80;
  server_name           $varname;
  rewrite     ^(.*)     https://$server_name$1 permanent;
}

I want to pass varname to the nginx.conf file to set the server name based on the users input. How can I do this?

Bodo :

You could create the file nginx.conf from a heredoc.

#!/bin/bash
# Ask the user for their name
echo What is the name of your domain?
read varname

cat > nginx.conf <<EOF
server {
  listen                80;
  server_name           $varname;
  rewrite     ^(.*)     https://\$server_name\$1 permanent;
}
EOF

echo You passed the domain name to your domain $varname successfully

Note: In the rewrite line I escaped the $ characters to get literal $ in the output instead of shell variable expansion.

If I enter foobar, this results in a file nginx.conflike this:

server {
  listen                80;
  server_name           foobar;
  rewrite     ^(.*)     https://$server_name$1 permanent;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=33027&siteId=1