0

I have found something similar on here but not exactly what I am looking for.
I am essentially trying to automate the creation of an nginx file which may have multiple domains attached to it.

My input looks like this:

./setup.sh domain1.com domain2.com domain3.com domain4.com domain5.com

What I am trying to do:

  1. Prepend www. to each of the input domain names (on the same line)
  2. Output all of it to a file.

Currently this breaks:

   SERVER_HOSTNAME=${1}
   shift
   SERVER_DOMAINS=(${@})

   echo '
   server {
           listen 80;
           listen [::]:80;

           root /var/www/${1}/html;
           index index.html index.php index.htm index.nginx-debian.html;

           server_name ${@} '
           for i in "${@}"; do echo "www.${i}"; done
           echo '
           more lines here.
           more
           more
           more

   ' > output.text

Currently this loop kind of works except it puts the www.$@ on separate lines and only outputs the 2nd part of the echo statement to the file.

Any ideas?

3
  • Have you considered breaking it down into multiple echos, one for the header, one for the inside content, and one for the footer? That's more efficient than what a lot of the answers are telling you to do (creating a subshell during heredoc invocation); those answers will work, but they do so at a performance penalty. Commented Jul 24, 2022 at 15:25
  • Note that you can make a redirection apply to a compound command: { echo "first"; for item in "$@"; do echo "second $item"; done; echo "$last"; } >output.text -- so the need to use > doesn't force you to make it all one echo. Commented Jul 24, 2022 at 15:26
  • please update the question to show a) the (wrong) output generated by your code and b) the (correct) expected output Commented Jul 24, 2022 at 16:16

1 Answer 1

2

You could use a heredoc and a bash parameter expansion:

#!/bin/bash

(( $# > 0 )) || exit 1

cat <<EOF
server {
    listen 80;
    listen [::]:80;

    root /var/www/$1/html;
    index index.html index.php index.htm index.nginx-debian.html;

    server_name ${@/#/www.};

    more lines here.
    more
    more
    more
}
EOF

So that:

./setup.sh domain1.com domain2.com domain3.com domain4.com domain5.com

Would output:

server {
    listen 80;
    listen [::]:80;

    root /var/www/domain1.com/html;
    index index.html index.php index.htm index.nginx-debian.html;

    server_name www.domain1.com www.domain2.com www.domain3.com www.domain4.com www.domain5.com;

    more lines here.
    more
    more
    more
}
Sign up to request clarification or add additional context in comments.

3 Comments

Wow, Nice! I ended up using Printf instead which more or less solved my issue but yours is much more elegant. Thank you!
how would you save this output to a file?
you can redirect the output in the first line cat <<EOF > output.text

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.