1

I use the following script to do the following actions in my environment:

  1. Creating an Nginx site conf.
  2. Creating a corresponding Let'sEncrypt SSL certificate.
  3. Creating a symlink between my sites-default dir to my site conf.
  4. Restarting the server.

My code:

#!/bin/sh
for domain; do
    > "/etc/nginx/sites-available/${domain}.conf" cat <<EOF
        server {
            root /var/www/html/${DOMAIN};
            server_name ${DOMAIN} www.${DOMAIN};

            location ~ /\.ht {
                deny all;
            }

            location / {
                index index.php index.html index.htm fastcgi_index;
                try_files $uri $uri =404 $uri/ /index.php?$args;
            }

            location ~*  \.(jpg|jpeg|png|gif|ico|css|js|ttf|woff|pdf)$ {
                expires 365d;
            }

            location ~ \.php$ {
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
            }
        }
    EOF
    sudo ln -s /etc/nginx/sites-available/${domain} /etc/nginx/sites-enabled/
    #########################################################################
    certbot --nginx -d ${DOMAIN} -d www.${DOMAIN}
done
systemctl restart nginx.service # Create Webapp Substrate.

How and why I want to improve this code:

Where the long row of hashmarks, I want to insert the following command:

echo 'create database ${DOMAIN}; create user "${DOMAIN}"@"localhost" identified by "mypassword"; grant all privileges on ${DOMAIN}.* to ${DOMAIN};' | mysql -u root -p

Adding this command will change the script to do 5 steps instead 4:

  1. Creates an Nginx site conf.
  2. Creates a corresponding Let'sEncrypt SSL certificate.
  3. Creating a symlink between my sites-default dir to my site conf.
  4. Adding an all privileged, and authenticated DB user suitable only for localhost, with a DB with the same name.
  5. Restarting the server.

My question:

As you can see, I used my parameter ${DOMAIN} inside the echo. I did that because I must pass the same argument there as well, otherwise, the DB username and it's associated DB both won't fit my site conf and my Wordpress based site won't boot.

Is this operation logical in Bash or you would take another approach?

To clarify, I ask this to understand if passing arguments based on parameters is valid when the parameter is inside an echo.

2 Answers2

3

You can do it without echo:

mysql -u root -ppass <<EOF
  create database ${DOMAIN};
  create user "${DOMAIN}"@"localhost" identified by "mypassword";
  grant all privileges on ${DOMAIN}.* to ${DOMAIN};
EOF

And see topic Why is printf better than echo?

  • Deep thanks Egor. Is there a way to write or paste mypassword on place, without writing anything like mypassword or Mps59azopp1? inside my script? (Of course, Mps59azopp1 isn't really my password, it's just a random example I made up to demonstrate that I prefer not to keep my DB particular password inside my script. – Arcticooling Nov 10 '17 at 04:02
  • One possible solution is to keep it mypassword and change it manually after the script finished running with SET PASSWORD FOR 'user-name-here'@'hostname-name-here' = PASSWORD('new-password-here'); while the user-name is what I've putted in $domain. – Arcticooling Nov 10 '17 at 04:04
  • 1
    @Arcticooling, you can leave -p without a password string, and when executing the script, it will ask for a password. Try to test this approach – Egor Vasilyev Nov 10 '17 at 06:18
  • So instead identified by "mypassword"; it becomes identified by -p;? – Arcticooling Nov 16 '17 at 05:57
  • see this topic: https://stackoverflow.com/questions/20471757/store-mysql-password-in-bash-script – Egor Vasilyev Nov 16 '17 at 08:15
2

It sounds like what you're asking about is Bash parameter substitution. The echo command doesn't play a role here. It looks like you're using single-quotes instead of double-quotes, which suppresses parameter substitution in the string. Just use double-quotes instead of single-quotes and use backslashes to escape any double-quotes that you want included in the string, e.g.:

echo "create database ${DOMAIN}; create user \"${DOMAIN}\"@\"localhost\" identified by \"mypassword\"; grant all privileges on ${DOMAIN}.* to ${DOMAIN};"
igal
  • 9,886