0

I've to find and replace default values of secret keys in wp-config-sample.php of Wordpress with the secret keys retrieved from the URL https://api.wordpress.org/secret-key/1.1/salt/ using shell.

Original string:
define( 'AUTH_KEY', 'put your unique phrase here' );

Replacement string (example):
define( 'AUTH_KEY', 'CA%c=@+l}j8uV%d6-LolGi~pFX(9Tqi+QwPjod:FRz0h.Hgi:OzG4/>MB,[17Lfd' );

I'm facing sed: -e expression #1, char 2: unterminated `s' command replacing the original string with the new string using sed with the following code:

wp_salts=$(curl -s https://api.wordpress.org/secret-key/1.1/salt/)
wp_auth_key_old="define( 'AUTH_KEY',         'put your unique phrase here' );"
wp_auth_key_new=$(echo "${wp_salts}" | grep -w 'AUTH_KEY')
sed -i -e $(printf 's/"${%q}"/"${%q}"/g' "$wp_auth_key_old" "$wp_auth_key_new") /var/www/html/wp-sample-config.php


I've tried many approaches but always end up with error due to substitution string being dynamic and having too many special characters.

Moreover neither does this work:
sed -i -e "s/\"$wp_auth_key_old\"/\"$wp_auth_key_new\"/g" /var/www/html/wp-sample-config.php

Nor this one:
sed -i -e "s/${wp_secret_key_old}/${wp_secret_key_new}/g" /var/www/html/wp-sample-config.php

NullPointer
  • 195
  • 4
  • 18
  • see also: https://unix.stackexchange.com/questions/129059/how-to-ensure-that-string-interpolated-into-sed-substitution-escapes-all-metac – Sundeep May 27 '20 at 06:10
  • The replacement string almost has all the special characters. The delimiter itself can be in the replacement string. Hence above solution does not work. – NullPointer May 27 '20 at 07:18
  • all of those are escaped... can you show a minimal example that fails along with the exact command you tried? – Sundeep May 27 '20 at 07:20
  • if you followed the unix link above, you can see `s:[\/&]:\\&:g` --> here `/` is present in the character class, thus it escapes the delimiter too – Sundeep May 27 '20 at 07:22

0 Answers0