3

I am trying to find {AUTH-KEYS-SALTS} in the file wp-config.php and replace it with the contents of the bash variable keysalts.

keysalts=`curl -sS https://api.wordpress.org/secret-key/1.1/salt/`
sed -i "s/{AUTH-KEYS-SALTS}/$keysalts/g" wp-config.php

The following almost works, except that keysalts has a bunch of special characters such as $`;'" and sed is getting confused. Basically, how do I escape everything and just replace {AUTH-KEYS-SALTS} with $keysalts?

Thanks.

Justin
  • 42,716
  • 77
  • 201
  • 296
  • If using bash, there's no need to deal with sed for string replacement. Just use: `(contents="$(cat wp-config.php)"; result="${contents//'{AUTH-KEYS-SALTS}'/"$keysalts"}"; echo "$result" >wp-config.php;)`. – Dennis Estenson Dec 29 '15 at 23:04

4 Answers4

3

Other way could be using perl:

perl -i -pe '
  BEGIN { 
    $keysalts = qx(curl -sS https://api.wordpress.org/secret-key/1.1/salt) 
  } 
  s/{AUTH-KEYS-SALTS}/$keysalts/g
' wp-config.php
Birei
  • 35,723
  • 2
  • 77
  • 82
  • The output of this was just outputted to standard out, how do I write the output of the perl to the file `wp-config.php`? – Justin Feb 24 '12 at 20:44
  • @Justin: With `-i` switch. Updated the answer. – Birei Feb 24 '12 at 20:53
  • @Birei How could this answer be modified to work with a standard `wp-config-sample.php` instead of just replacing {AUTH-KEYS-SALTS}. Some sort of foreach loop on each define? – Brandon Jan 28 '14 at 20:03
  • @Brandon: I don't understand it. You should create a new question with your problem, that seems different of this one. – Birei Jan 28 '14 at 21:19
  • @Birei Here is my question http://stackoverflow.com/questions/21417651/perl-salt-generation-on-wp-config-sample-php – Brandon Jan 28 '14 at 21:57
2

You can escape $keysalts using sed itself:

sed -i "s/{AUTH-KEYS-SALTS}/`echo $keysalts | sed -e 's/[\/&]/\\&/g'`/g" wp-config.php

See this question for more information.

Community
  • 1
  • 1
Eduardo Ivanec
  • 11,668
  • 2
  • 39
  • 42
  • There are some characters that can still cause trouble in the `echo` command (`?`, `*`, multiple spaces, ...). Use `echo "$keysalts"` or better yet `printf "%s" "$keysalts"` to avoid these problems. – Gordon Davisson Feb 24 '12 at 20:48
1

It turns out you're asking the wrong question. I also asked the wrong question. The reason it's wrong is the beginning of the first sentence: "In my bash script...".

I had the same question & made the same mistake. If you're using bash, you don't need to use sed to do string replacements (and it's much cleaner to use the replace feature built into bash).

Instead of:

  keysalts=`curl -sS https://api.wordpress.org/secret-key/1.1/salt/`
  sed -i "s/{AUTH-KEYS-SALTS}/$keysalts/g" wp-config.php

you can use bash features exclusively, reading the file into a variable, replacing the text in the variable, and writing the replaced text back out to the file:

  INPUT=$(<wp-config.php)
  keysalts="$(curl -sS https://api.wordpress.org/secret-key/1.1/salt/)"
  echo "${INPUT//'{AUTH-KEYS-SALTS}'/"$keysalts"}" >wp-config.php
Dennis Estenson
  • 1,022
  • 10
  • 11
1

Actually, I like this one better

sed -i 's/{AUTH-KEYS-SALTS}/'"$keysalts"'/g' wp-config.php

Personal choice. :-)

Community
  • 1
  • 1
macduff
  • 4,655
  • 18
  • 29