2014-11-04 77 views
2

我有一个脚本更新盐Shell脚本

#!/bin/bash 
chars=({a..z} {A..Z} {0..9} \, \; \. \: \- \_ \# \* \+ \~ \! \§ \$ \% \& \(\) \= \? \{ \[ \] \} \| \> \<) 

function rand_string { 
local c=$1 ret= 
while((c--)); do 
ret+=${chars[$((RANDOM%${#chars[@]}))]} 
done 
printf '%s\n' "$ret" 
} 
for i in {1..8} 
do 
export salt$i=$(rand_string 64) 
done 

echo $salt1 
echo $salt2 

sed -i 's/put your unique phrase here/'$salt1'/g' /var/www/testsite/wp-config.php 
sed -i 's/put your unique phrase here/'$salt2'/g' /var/www/testsite/wp-config.php 

我看到我的屏幕输出是:

[[email protected] testsite]# ./salt.sh 
$,E5:2nB$p>s;2Y)h>}VU2V5Lj2UC38,yY+0?{LM;rt;Xx%B:#%9z2oc-BSE;1%Z 
~#&2#p#hZkT+)p83ZD%7=noy?§xbx%3iP4*|z<-haHXVIk<[IR0>v%.r*+X5=kNl 

但是我的wp-config.php文件包含

define('AUTH_KEY',   '$,E5:2nB$p>s;2Y)h>}VU2V5Lj2UC38,yY+0?{LM;rt;Xx%B:#%9z2oc-BSE;1%Z'); 
define('SECURE_AUTH_KEY', '$,E5:2nB$p>s;2Y)h>}VU2V5Lj2UC38,yY+0?{LM;rt;Xx%B:#%9z2oc-BSE;1%Z'); 

我也尝试将SED语句放入我的循环中,但无法弄清楚如何调用变量。

sed -i 's/put your unique phrase here/'$salt$i'/g' /var/www/testsite/wp-config.php 

的wp-config.php文件包含

define('AUTH_KEY',   '1'); 
define('SECURE_AUTH_KEY', '1'); 

我怎样才能解决这个问题。

我想看到什么是

define('AUTH_KEY',   '$,E5:2nB$p>s;2Y)h>}VU2V5Lj2UC38,yY+0?{LM;rt;Xx%B:#%9z2oc-BSE;1%Z'); 
define('SECURE_AUTH_KEY', '~#&2#p#hZkT+)p83ZD%7=noy?§xbx%3iP4*|z<-haHXVIk<[IR0>v%.r*+X5=kNl'); 

回答

0

put your unique phrase here是两条线。第一个sed取代了这两个实例。

像这样的东西应该工作:

sed -i -e '/put your unique phrase here/{s/put your unique phrase here/'"$salt1"'/;N;s/put your unique phrase here/'$salt2'/}' wp-config.php 

只有在该字符串相匹配的线路运行命令。匹配时执行第一次替换,将下一行读入模式空间并执行第二次替换。