0

I have a bit of bash that I use to "install" a wordpress site on a server.

In it, I do a curl request to get the randomized SALTs via SALTS=$(curl -L https://api.wordpress.org/secret-key/1.1/salt/);

And I put a "token" in my wp-config.php file ##WP_SALTS##

It seems that my sed statement is not proper, as it keeps throwing me an error:

sed: -e expression #1, char 99: unknown option to `s'

I have tried:

sed -i "s+##WP_SALTS##+$SALTS+g" $WPCONFIG

As well:

sed -i "s/##WP_SALTS##/$SALTS/g" $WPCONFIG

Still the error presents. What am I doing wrong?

the response from the curl request are random each time, but are similar to:

define('AUTH_KEY', ':a>?5od_kaveFKaIB8px|!vgF-W/6/AQX04=&>Tu.-q3ehGOh59=SX+qc9sWk|gG'); define('SECURE_AUTH_KEY', ':tw+w! Sn2n~Rt0ReVA6#eWqsUXW5elHo@V~ oiRhTH4k]kg{<k`:An`]z==K@wZ'); define('LOGGED_IN_KEY', 'mD,JT*4pa3}zfGEpXFR}9jlzF,iD ;:]|>yu]T}&8Uy~(-5ml/AEBTG4|7QYCB|j'); define('NONCE_KEY', '@{Q`.7T a)S?0DTutE}D5Is(UlwnG4NuoQiFHas&i@qz%-HTd7-8[v50Nx<]akuT'); define('AUTH_SALT', 'es*7hCVnh/+c-cecgmZ?%QZ_KN^kaA[jD]N}{A8sK|~MH@Vl|(6-|{3EIGMhksy['); define('SECURE_AUTH_SALT', '}AF@:i!hy#C5,Q_5c4yhycm~i|fc53@+|h7r5H9y(/&4VBeX&sOrKC-6+AqeZ|L>'); define('LOGGED_IN_SALT', '4,}H+&[@qN#^!B+?3a+Mh0+?pURhP|v.CV/]4F-6G!TncU*Pd=GMSRPf?58j5Sv0'); define('NONCE_SALT', 'Dlo,7F[:EaWQT57-P0Q+x</nUf4UD&LH=-0wS6l._2Fx!-jR0KBJ-U_1*{sXo?>Q');

EDIT Now implementing a way to randomly generate the 64 character salts with the following:

RSTR=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w ${1:-64} | head -n 1);
SALTS="define('AUTH_KEY', '$RSTR');
define('SECURE_AUTH_KEY', '$RSTR');
define('LOGGED_IN_KEY', '$RSTR');
define('NONCE_KEY', '$RSTR');
define('AUTH_SALT', '$RSTR');
define('SECURE_AUTH_SALT', '$RSTR');
define('LOGGED_IN_SALT', '$RSTR');
define('NONCE_SALT', '$RSTR');";
sed -i "s/##WP_SALTS##/$SALTS/g" $WPCONFIG

I've replaced the delimiter with +, ~, /, &, * and they all still generate this same error.

Kevin
  • 2,684
  • 6
  • 35
  • 64

3 Answers3

2

If SALTS may contain any character it can be done with perl.

Passing by environment :

SALTS="$SALTS" perl -i.bak -pe 's/##WP_SALTS##/$ENV{SALTS}/g' "$WPCONFIG"

Passing by arguments :

perl -i.bak -pe 'BEGIN{$salts=shift}s/##WP_SALTS##/$salts/g' "$SALTS" "$WPCONFIG"
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36
  • The could also just export the SALT variable when it is set. – 123 Sep 15 '17 at 13:00
  • Indeed, the first could also be `export SALTS; perl -i.bak -pe ...` – Nahuel Fouilleul Sep 15 '17 at 13:18
  • 1
    This works because $salt is a perl variable and is replaced by interpreter whereas in bash + sed, is replaced by bash and sed can't interpret correctly for example because of delimiter or newline characters inside variable – Nahuel Fouilleul Sep 15 '17 at 13:54
1

$SALTS may contain the character used as delimiter (+ or /). If your file doesn't contain ~ you could try:

sed -i "s~##WP_SALTS##~$SALTS~g" $WPCONFIG

But you can use any printable character as delimiter.

With GNU sed, you can also choose a non-printable one (see "sed rare-delimiter (other than & | / ?…)".

SLePort
  • 15,211
  • 3
  • 34
  • 44
0

When you need a literal string, don't use sed as it only understands regexps and replacement strings with backreferences and has delimiter constraints. Just use a tool with string functions like awk:

SALTS="$SALTS" awk -i inplace '
BEGIN { old="##WP_SALTS##""; lgth=length(old); new=ENVIRON["SALTS"] }
s=index($0,old) { $0=substr($0,1,s-1) new substr($0,s+lgth) }
{ print }
' "$WPCONFIG"

The above uses GNU awk for inplace editing with -i inplace, just like your GNU sed was doing with -i.

Don't use all upper case variable names for non-exported variables btw - that's by convention and to avoid clashes with exported and builtin variables.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185