0

I want to add this code

$cfg['Servers'][$i]['hide_db'] = '^(mysql|information_schema|performance_schema|phpmyadmin)$';

into phpMyAdmin's config.inc.php file before the line

/**
 * End of servers configuration
Expect result:
$cfg['Servers'][$i]['hide_db'] = '^(mysql|information_schema|performance_schema|phpmyadmin)$';

/**
 * End of servers configuration
 */

Here is sample of config.inc.php file ( https://github.com/phpmyadmin/phpmyadmin/blob/master/config.sample.inc.php )

My current sed code in .sh file is

#!/bin/sh

PHPMATARGETDIR="/var/www/phpmyadmin"

sudo sed -i "s/\(\/\*\*\)/ #my code before;\n\1/" ${PHPMATARGETDIR}/config.inc.php

but it is not working, it's just prepend to all open comment block.

If I use this code then it is not working at all.

sudo sed -i "s/\(\/\*\*\n\s*\* End of servers configuration\)/ #my code before;\n\1/" ${PHPMATARGETDIR}/config.inc.php

1
  • sed works on a single line at a time, so your "not working at all" solution is doomed to failure. It can probably be rescued by saying "every time you see an open comment block, append the next line onto the pattern space".
    – icarus
    Commented Aug 15, 2021 at 15:44

2 Answers 2

1

You could do this in sed:

sed "/\/\*\*/{
N
/ \* End of servers config/i\
\$cfg['Servers'][\$i]['hide_db'] = '^(mysql|information_schema|performance_schema|phpmyadmin)\$';
}" config.inc.php

Note that your provided config.inc.php actually contains

/*
 * End of servers configuration
 */

without the double-asterisk - it would probably be safer to make the first expression /\/\*/ and the second simply /End of servers config/

4
  • Sorry, the original by phpMyAdmin is /** not /*. I'll test your code today. Thank you
    – vee
    Commented Aug 16, 2021 at 2:48
  • It seems to work but it just echo out without write to the file.
    – vee
    Commented Aug 16, 2021 at 5:21
  • @vee From reading your question, you seem to know how to use -i with sed.
    – Kusalananda
    Commented Aug 16, 2021 at 7:04
  • 1
    @Kusalananda Oh, thanks. I have to add sudo before sed and use -i with that.
    – vee
    Commented Aug 16, 2021 at 8:28
0

The solution for me was to use perl bash as taken from here and this reference.

PHPMATARGETDIR="/var/www/phpmyadmin"

ADDPMACONFIG="\$cfg['Servers'][\$i]['hide_db'] = '^(mysql|information_schema|performance_schema|phpmyadmin|sys)\$';"
ADDPMACONFIG=$(printf '%s\n' "$ADDPMACONFIG" | sed -e 's/[]\/$*.|()^[]/\\&/g')
sudo perl -0777 -i -p -e "s/(\/\*\*\n\s+\*\s+End of servers configuration.*)/${ADDPMACONFIG}\n\1/" ${PHPMATARGETDIR}/config.inc.php

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .