I was trying to create a script that installs Wordpress automatically in Ubuntu Server, for a college project. The problem comes when I try to insert the authentication keys inside of the wp-config.php file. The keys are obtained through an url that generates them, and can be obtained using curl. The output of the command: curl -s https://api.wordpress.org/secret-key/1.1/salt/ is something like this:
define('AUTH_KEY', '$+ef);AyI`T!+<2gPHrG%R^&6NX`E~+TumT qdMWY&QP.l) TYz2fENG}[bCc8~:');
define('SECURE_AUTH_KEY', 'omDNxow|,(WIuk-pS MRG&P&2Shlk8Y=Q<PH|+fA;T`~b:?I- <Q|ng}~TX|@=^s');
define('LOGGED_IN_KEY', '3d[bU]VTc+R84#+~TYAO#?c+FQLWVxDq&1wGUZC;$d@n1)9We VXrGIJN3cX*d%E');
define('NONCE_KEY', 'S(No,u/>6(Q4!2(u6ri-LRAp8uESFY{__ZWbH(nP]YOC3WA@JvWkkwrIY<^Dvm b');
define('AUTH_SALT', 'ph512NCyJ?3]g|a|<5@fH5cCX&Rw{%+}Sm?gmVQ6WRnj-]N2]rg+i{21<-05`H>o');
define('SECURE_AUTH_SALT', 'hig|pYFPmF6w1Jm&R:U{ZDvxkeOW*#63ys!b7]FT|v*Crm>8kK!;hw=`hovm)Rqp');
define('LOGGED_IN_SALT', 'S[40r?y~.*-]Z9]0qLEOTp}}+T|lO|p-Qz|^h}:C{s&_X]kOkPp+uKuOhy3j`sf9');
define('NONCE_SALT', 'Ph|CZMq3C]<p2*G=qV2PSj5o4mYjTg!NX`[G---_W%w2H:`e6Q@1!a:]vYWxQlP#');
And the default values look like this: define( 'AUTH_KEY', 'put your unique phrase here' );
I want to insert the keys in the same lines where the sample lines are. If I just append the output of curl to the document, it won't work. The problem is that the keys are formed by a lot of conflictive characters, which cause errors when I try to replace the lines using sed. The bash script I have done to replace the lines doesn't work because of the conflictive characters. This is the script:
key_names=("AUTH_KEY SECURE_AUTH_KEY LOGGED_IN_KEY NONCE_KEY AUTH_SALT SECURE_AUTH_SALT LOGGED_IN_SALT NONCE_SALT")
key_values=($(curl -s https://api.wordpress.org/secret-key/1.1/salt/ | awk -F"'" '{for(i=2;i<=NF;i+=2) print $i}' | awk 'length >30'))
# iterate over each element to insert the keys in wp-config.php proper line
for ((i=0; i<${#key_values[@]}; i++)); do
sed -i "s/define( '${key_names[i]}', '.*' );/define( '${key_names[i]}', '${key_values[i]}' );/" $WP_CONF_FILE
done
I tried by just redirecting the output of the curl command to the file, but it doesn't work, the lines need to be inserted where the example lines are. I also tried to replace the lines with sed (using the script above), but it doesn't work because of the conflictive characters. I also tried asking ChatGPT, but it didn't really come up with any solution.
Any idea of how could I solve this? Thank you!
sedusually isn't the best choice for substitutions using input values since it doesn't understand literal strings, see Is it possible to escape regex metacharacters reliably with sed for details. Also using a shell loop is very slow, as is runningsedmultiple times on your input file, and it introduces rainy day cases where a subsequent substitution might replace a change made by an earlier substitution.I think this might be what you're trying to do, using any awk and just modifying the input file once for all keys together:
When I ran your
curlcommand from the command-line I got the same kind of output you show but when I ran it from a script I got no output so when I removed the-soption to see if it was reporting any failures I getcurl: (35) schannel: next InitializeSecurityContext failed: Unknown error (0x80092012) - The revocation function was unable to check revocation for the certificate.. I had to add-kto skip certificate validation to get it to work from a script.Copying an example file google found on github to use as sample input:
I tested my script using:
By the way, in your original script there was no need to hard-code an array
key_names[]when you could've just read the names from thecurloutput like you're reading the values, e.g. with 2 indexed arrays as you have:or a single associative array:
Also
| awk -F"'" '{for(i=2;i<=NF;i+=2) print $i}' | awk 'length >30')could've just been| awk -F"'" '{print $4}'or even| curl -d"'" -f4.