Sed更换令牌的问题

问题描述:

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.

我有一些bash用于在服务器上“安装”wordpress网站。 p>

在其中,我通过 SALTS = $(curl -L https://api.wordpress.org/secret-key/1.1/salt/)进行卷曲请求以获取随机化的SALT ; code> p>

我在wp-config.php文件中加了一个“令牌” ## WP_SALTS ## code> p> \ n

似乎我的 sed code>语句不正确,因为它不断给我一个错误: p>

  sed:-e expression#1  ,char 99:未知选项`s'
  code>  pre> 
 
 

我尝试过: p>

  sed -i“s +  ## WP_SALTS ## + $ SALTS + g“$ WPCONFIG 
  code>  pre> 
 
 

同样: p>

  sed -i  “s / ## WP_SALTS ## / $ SALTS / g”$ WPCONFIG 
  code>  pre> 
 
 

仍然出现错误。 我做错了什么? p>

curl code>请求的响应每次都是随机的,但类似于: p>

  define('AUTH_KEY',':a&gt;?5od_kaveFKaIB8px |!vgF-W / 6 / AQX04 =&amp;&gt; Tu.-q3ehGOh59 = SX + qc9sWk | gG');  define('SECURE_AUTH_KEY',':tw + w!Sn2n~Rt0ReVA6#eWqsUXW5elHo @ V~oiRhTH4k] kg {&lt; k`:An`] z == K @ wZ');  define('LOGGED_IN_KEY','mD,JT * 4pa3} zfGEpXFR} 9jlzF,iD;:] |&gt; yu] T}&amp; 8Uy~(-5ml / AEBTG4 | 7QYCB | j'); define('NONCE_KEY',  '@ {Q`.7T a)S?0DTutE} D5Is(UlwnG4NuoQiFHas&amp; i @ qz%-HTd7-8 [v50Nx&lt;] 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(/&amp; 4VBeX&amp; sOrKC-6 + AqeZ | L&gt;');定义('LOGGED_IN_SALT','4,} H +&amp; [@ qN#^!B +?3a + Mh0 +?pURhP | v.CV  !/] 4F-6G TncU * PD = GMSRPf 58j5Sv0 ');定义(' NONCE_SALT '' DLO,7F [:?!EaWQT57-P0Q + X&LT; / nUf4UD&安培; LH = -0wS6l._2Fx -jR0KBJ-U_1 * {  sXo?&gt; Q'); 
  code>  pre> 
 
 

编辑 strong> 现在实现一种随机生成64个字符盐的方法:

  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('SEC  URE_AUTH_SALT','$ RSTR'); 
define('LOGGED_IN_SALT','$ RSTR'); 
define('NONCE_SALT','$ RSTR');“; 
sed -i”s / ## WP_SALTS ## /  $ SALTS / g“$ WPCONFIG 
  code>  pre> 
 
 

我用+,〜,/,&amp;,*替换了分隔符,它们仍然会产生同样的错误。 p> div>

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"

$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 & | / ?…)".

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.