2

I am having a hard time trying to make sed work to apply the substitution operation bellow.

SALT=$(curl -L https://api.wordpress.org/secret-key/1.1/salt/)
#SALT='test' #this of course works fine
#echo $SALT
sed -i "s/salt_here/$SALT/g" wp-config.php

Inside the file "wp-config.php" there is a line with the word "salt_here" (without quotes). However, the $SALT variable has a lot of garbage, so I am getting this error:

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

Is there a way to escape all the garbage inside a variable (just like php's http://php.net/preg_quote)

UPDATE:

This is the best I can get:

SALT=$(curl -L https://api.wordpress.org/secret-key/1.1/salt/ | sed 's/\(\/\|\\\|&\)/\\&/g')
SALT1=`/usr/bin/php << EOF
<?php echo preg_quote("$SALT"); ?>
EOF`
#echo "$SALT1"
cp '/home/public_html/zz_f/wp-config.php' '/home/public_html/zz_f/wp-config_t.php'
sed -i "s/salt_here/$SALT1/g" wp-config.php

But still with an error:

PHP Parse error:  syntax error, unexpected '>', expecting T_VARIABLE or '$' in - on line 3
sed: -e expression #1, char 12: unterminated `s' command
Roger
  • 8,286
  • 17
  • 59
  • 77

2 Answers2

0

I'm writing a bash script to automate wp installs && am stuck on this issue as well.

One thing I noticed is that you might want to try this :

sed -i 's/salt_here/'$SALT'/g' wp-config.php

I was having problems passing variables in as well, but adding the quotes around the variable seem'd to remove that issue [ note: not a sed or bash expert, so not sure how this would translate w/ double quotes .. ]

flash
  • 115
  • 1
  • 6
0

Use sed to quote it.

SALT=$(curl -L https://api.wordpress.org/secret-key/1.1/salt/ | sed 's/\(\/\|\\\|&\)/\\&/g' )
Brilliand
  • 13,404
  • 6
  • 46
  • 58
  • But when I try to run `sed -i 's/salt_here/$SALT/g' wp-config.php` it retuns the same error: `sed: -e expression #1, char 147: unterminated `s' command` – Roger Jun 03 '11 at 20:03
  • Hmm, that's not the same error. Before it was being terminated too soon, now it isn't being terminated at all. Try echoing that last command instead of running it to find out what's going on. – Brilliand Jun 03 '11 at 20:17
  • I've edited my answer based on the accepted answer to the question that hakre linked to. Apparently it was a mistake to escape digits, and I don't know whether there was anything else that I shouldn't have been escaping. – Brilliand Jun 03 '11 at 20:37
  • This is the error I am getting now: `sed: -e expression #1, char 107: unterminated `s' command` – Roger Jun 03 '11 at 21:55