PHP's preg_quote equivalent in Bash?
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开发者_StackOverflow中文版/$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
Use sed to quote it.
SALT=$(curl -L https://api.wordpress.org/secret-key/1.1/salt/ | sed 's/\(\/\|\\\|&\)/\\&/g' )
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 .. ]
精彩评论