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:
- add www before each of the input domain names (on the same line)
- 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?
CodePudding user response:
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
}