0

I am trying to write a script that automatically makes a new website on my server.

one of the steps is to go to this link and copy the salts into my wp-config.php file:

https://api.wordpress.org/secret-key/1.1/salt/

I'm new to bash script and hoping this would be easy to do.

I'm not sure if I need to save it as a text first, but ultimately I need to replace this part in my wp-config.php:

define( 'AUTH_KEY',         'put your unique phrase here' );
define( 'SECURE_AUTH_KEY',  'put your unique phrase here' );
define( 'LOGGED_IN_KEY',    'put your unique phrase here' );
define( 'NONCE_KEY',        'put your unique phrase here' );
define( 'AUTH_SALT',        'put your unique phrase here' );
define( 'SECURE_AUTH_SALT', 'put your unique phrase here' );
define( 'LOGGED_IN_SALT',   'put your unique phrase here' );
define( 'NONCE_SALT',       'put your unique phrase here' );

with the results from that link.

user3486773
  • 1,174
  • 3
  • 25
  • 50
  • Does this answer your question? [How do I download a file from the internet to my linux server with Bash](/q/14300794/4518341). In short, use `wget` or `curl`. – wjandrea Jul 24 '22 at 22:34

1 Answers1

0

Using awk

$ cp wp-config.php wp-config.bak
$ curl https://api.wordpress.org/secret-key/1.1/salt/ | awk -F"'" 'NR==FNR {a[$2]=$4;next} {$4=a[$2]}1' OFS="'" - wp-config.php > wp-config.tmp
$ mv wp-config.tmp wp-config.php
$ cat wp-config.php
define( 'AUTH_KEY',         '+koF5XB%cw@d[0-ki.7, L<Jjun`r7U)e]N4T`x` -PgG|MBDjVKqn|v;F f9]?1' );
define( 'SECURE_AUTH_KEY',  'zk_kt$P|41U-|Vz:r&wfJW=b6D=_?c`=3.`v1n~K~u1-1Qp|]7&4q:8URK+7i)a+' );
define( 'LOGGED_IN_KEY',    '_R.$kMfRnn6jm#hBN-C04610P.Yg.mxLiaSjH{}9%4c0>/{uiAk+}9oCpfj_<wnJ' );
define( 'NONCE_KEY',        '40*|=BJN0qo=8O2~x4Rz.A~I+gsF,3UsF?6q/RK5k2;8Dd:o+6o~F&7S_y+^TL-;' );
define( 'AUTH_SALT',        'irwQFh*=:kyToG= aUF;+K%]$E-]]=^@V[l[7@u-pR;ea*Z+d$&YS]i|<-Gj[Bj^' );
define( 'SECURE_AUTH_SALT', ',/rVV-JlDv~`R8ocz*`+T^.CQF.`X3+dA?q5MZHVz(8>&H&r:A#XH?/XV>)L :8s' );
define( 'LOGGED_IN_SALT',   '8zf>mA|rmvH%2C`>acJe?*O-TEJ>f0F&K82)Q4u{T1lOH#kG%IbO5>Vw1Bf9b-98' );
define( 'NONCE_SALT',       'p,6nSXBfw~4EySqIu!fp):B1ye1Hu{S{V)ef;m9X;/z}/UA/G|I:2|]Awx,K/k+,' );
HatLess
  • 10,622
  • 5
  • 14
  • 32
  • Why is the `awk` expression needed? If you want to add a space after `define(` and before `);`, that's easily doable in `sed` (can be done in `awk` too) However, I'm confused by your assignment `a[$2]=$4` followed by the assignment `$4=a[$2]` unless that's simply being done to remove unneeded whitespace (of which there is none for `$2` and `$4`) And why `NR==FNR`? All read is from `stdin` so `NR` will always equal `FNR` in this case?? – David C. Rankin Jul 25 '22 at 06:26
  • @DavidC.Rankin I am not trying to remove or add whitespace, however, $4 does also have white space. My code replaces the contents of $4 from the conf file with the contents of $4 from the link if $2 matches. – HatLess Jul 25 '22 at 08:01
  • I got it, my bad, it's clear. Coffee had more than worn off by then. – David C. Rankin Jul 25 '22 at 23:07