Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed with complex string

Tags:

bash

sed

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!

like image 246
Julien B. Avatar asked Jul 07 '26 11:07

Julien B.


1 Answers

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
like image 108
Julien B. Avatar answered Jul 10 '26 15:07

Julien B.