Reputation: 3324
I'm trying to sed into phpMyAdmin config file to add a blowfish key.
Here's what I have so far...
k=$(openssl rand -base64 32)
o="\$cfg['blowfish_secret'] = '';"
n="\$cfg['blowfish_secret'] = '$k';"
sed -i -e 's/'"$o"'/'"$n"'/g' /var/www/phpmyadmin/config.inc.php
I don't get any error, but it's not working as expected.
echo -e "$k\n$o\n$n"
returns
Jgcv3FlugcVi5rDYLCdNhxX6sEZatt0zTZV3PISiLJY=
$cfg['blowfish_secret'] = '';
$cfg['blowfish_secret'] = 'Jgcv3FlugcVi5rDYLCdNhxX6sEZatt0zTZV3PISiLJY=';
And...
cat /var/www/phpmyadmin/config.inc.php | grep blowfish
returns
$cfg['blowfish_secret'] = ''; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
Please help!
Upvotes: 3
Views: 805
Reputation: 3324
AS 123 mentioned in a comment [] characters were interpreted as character classes by sed, simply escaping it with backslash solved my problem.
So this :
k=$(openssl rand -base64 32)
o="\$cfg['blowfish_secret'] = '';"
n="\$cfg['blowfish_secret'] = '$k';"
sed -i -e 's/'"$o"'/'"$n"'/g' /var/www/phpmyadmin/config.inc.php
Was changed to that :
k=$(openssl rand -base64 32)
o="\$cfg\['blowfish_secret'\] = '';"
n="\$cfg\['blowfish_secret'\] = '$k';"
sed -i -e 's/'"$o"'/'"$n"'/g' /var/www/phpmyadmin/config.inc.php
And now it works.
EDIT :
Since openssl rand -base64 32 can generate a string with slashes, it's better to escape it right away. Also, I chose to use perl after all. Here's the corrected code:
k=$(openssl rand -base64 32 | sed 's,\/,\\/,g')
o="\$cfg\['blowfish_secret'\] = '';"
n="\$cfg\['blowfish_secret'\] = '$k';"
sudo perl -p -i -e "s/$o/$n/g" /var/www/phpmyadmin/config.inc.php
Upvotes: 2
Reputation: 785196
If you want to use all kind of special regex meta characters in search and replace strings here is a non-regex way of doing that using awk
:
# utility function to format search and replace strings
frmt() {
printf "\$cfg['blowfish_secret'] = '%s';" "$1"
}
# awk command to perform search and replace using plain text search
awk -v s="$(frmt "")" -v r="$(frmt "$k")" 'n=index($0, s) {
print r substr($0, n+length(s))
}' /var/www/phpmyadmin/config.inc.php
PS: Note that above awk
command doesn't do inline editing of the file. If you want to do that then use -i inline
option available with gnu-awk
as:
awk -i inline -v s="$(frmt "")" -v r="$(frmt "$k")" 'n=index($0, s) {
print r substr($0, n+length(s))
}' /var/www/phpmyadmin/config.inc.php
Upvotes: 0