2

I am writing a script that will install WordPress automatically for me, to make my life easier as I have to deploy WordPress installs constantly.

However, I am trying to make it so I never have to interact with it, and that is taking my sleep away.

To generate the passwords for databases and whatnot, I use apg, and then use sed to replace a string with the correct password. It looks something like this:

DOMAIN="$2"
DB_NAME_USER="$(echo $DOMAIN | sed -e 's/\.//g')"
DB_PASSWORD="$(apg -a 1 -n 1 -m 64 -d -c cl_seed -E \_\"\'\`\/\\\=)"

sed -i "s/database_name_here/$DB_NAME_USER/g" /srv/"$DOMAIN"/wp-config.php
sed -i "s/username_here/$DB_NAME_USER/g" /srv/"$DOMAIN"/wp-config.php
sed -i "s/password_here/$DB_PASSWORD/g" /srv/"$DOMAIN"/wp-config.php

Replacing the database and username works just fine, but when replacing the password, six out of ten times the replace goes bad and I end up with something like this:

define('DB_PASSWORD', 'Ymb{X-dT~l5Agg9q561A^?npassword_hereD;$U,s>mu*:f7|BK4?oM#OH>cHgsI9GndW@~SkBQ');

Emphasis on the issue. Instead of removing the "password_here" part and replacing it with the actual password, it kind fuses the string in the middle of the password.

I thought it could be a character generated from apg that was messing with sed, but I removed quite a few from the password generated using -E and still get this a lot.

Same happens with other replaces, always involved apg and sed. Replacing things such as _PASSWORD_HERE_ result in the same issue from time to time.

Is there any way around this or do I have to keep trying to find characters that cause issues and then add them to apg's exclusion list? Or maybe tell sed to replace the whole line instead of just the string I want?

Thanks in advance!

  • Why don't you just use the wp command? Rolling your own is too much work when it can do everything you need already. – Michael Hampton Dec 31 '18 at 18:27
  • The script handle a little more than just installing WordPress, like calling an API to add DNS records, create tmpfs for caching and other stuff, that is why WP-CLI alone won’t cut it. I mean, the wp-config.php file generation could be done with WP-CLI, but that would not fix the other times sed breaks with apg, like when generating a .my.cnf file with the database password for easier access. – James Pond Dec 31 '18 at 18:30
  • For the rest of it you probably should be using something like ansible anyway. – Michael Hampton Dec 31 '18 at 18:45
  • @MichaelHampton I plan to switch to Ansible later on, but right now I prefer to write this in bash because I clearly need to improve my bash skills, and writing this is a nice learning experience. – James Pond Dec 31 '18 at 18:54
4

You need to escape the \, / and & characters in your input otherwise sed acts upon them. Something like

DB_PASSWORD=$(echo "$DB_PASSWORD" | sed -e 's/[\/&]/\\&/g')
sed -i "s/password_here/$DB_PASSWORD/g" /srv/"$DOMAIN"/wp-config.php

should do the trick.

  • Hm. I do exclude / and \ but I forgot to exclude $. Might be related, time to test. Thanks! :) – James Pond Dec 31 '18 at 18:52
  • I don't mention $ at all.are you confusing it with & ? – user9517 Dec 31 '18 at 18:55
  • Sorry, that was a typo. Meant to say & :) – James Pond Dec 31 '18 at 18:58
  • Ran a couple times with option -E \/\\\& on apg and it worked like a charm, thanks a lot! I had no idea & caused problems with sed, which is why I never excluded it. – James Pond Dec 31 '18 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.