0

The name in the title says it all. However, I'm absolutely the worst with the sed command. So I'm trying to edit the following file: /var/www/html/phpMyAdmin/config.inc.php

I want to edit the line that says

$cfg['Servers'][$i]['AllowRoot'] = false;

into the following

$cfg['Servers'][$i]['AllowRoot'] = true;

It has so many special characters and whatnot and I have no prior knowledge of how sed works. So here's some commands I've tried to specifically edit that one line.

sed -i "/*.AllowRoot.*/\$cfg['Servers'][\$i]['AllowRoot'] = true;/" /var/www/html/phpMyAdmin/config.inc.php
sed -i "/*.AllowRoot.*/$cfg['Servers'][$i]['AllowRoot'] = true;/" /var/www/html/phpMyAdmin/config.inc.php
# this one finds the line successfully and prints it so I know it's got the right string:
sed -n '/AllowRoot/p' /var/www/html/phpMyAdmin/config.inc.php
sed -i "s/'AllowRoot|false'/'AllowRoot|true'/" /var/www/html/phpMyAdmin/config.inc.php

I have absolutely no idea what I'm doing and I'm not learning a whole lot besides the feeling that the last command splits up 'AllowRoot|false' makes sure that both must be present in the sentence to come back as a result. So to my logic, I thought changing the word false into true would make that happen, but nothing. The other commands return... bizarre results at best, one even emptying the file. Or that's one of the commands I had not written down here, I've lost track after 50 attempts. What is the solution here?

joanis
  • 10,635
  • 14
  • 30
  • 40
  • Yes, I am aware I shouldn't use root to login to phpmyadmin, but it's convenient for the short moment you'll be working with it, and then the goal is to set the flag to false again. – Patrickkasie Jun 20 '22 at 13:23
  • If you have "absolutely no idea what [you're] doing", maybe the right place to start is a basic `sed` tutorial. A little knowledge is dangerous. Executing commands with zero knowledge can be catastrophic. – William Pursell Jun 20 '22 at 14:37

3 Answers3

2

The [ and ] need to be escaped to match literal brackets, instead of inadvertently starting a bracket expression. This should work:

$ sed -i "/\$cfg\['Servers'\]\[\$i\]\['AllowRoot'\]/s/false/true/"  /var/www/html/phpMyAdmin/config.inc.php
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 1
    Upvoting this. Of course you need to escape brackets. My example just accidentally worked. Also did not know you could create text to match before 's', made expression nicer. – toftis Jun 20 '22 at 14:03
  • Me neither at what toftis said. Absolutely fantastic, works like a charm on steroids. Not enticing to do drugs, kids, but I have to admit that's a whole lot of escaping there. Thank you very much! – Patrickkasie Jun 20 '22 at 16:17
  • Addresses can be either line numbers or regular expressions matching the text of the line. – chepner Jun 20 '22 at 16:36
0

There is not many things to escape in sed. Main problem in your line is / which you have chosen as delimiter (most common, but not required). I suggest you use # and the following will work:

sed -i "s#$cfg['Servers'][$i]['AllowRoot'] = false;<br />#$cfg['Servers'][$i]['AllowRoot'] = true;<br />#g" input.txt

however you need to think about bash interpreter as well. $i and $cfg will be interpreted as variables. My suggestion is that when you want to match a string like this to put the sed expression in a text file like this:

cat allow_root_true.sed
s#['Servers'][]['AllowRoot'] = false;<br />#['Servers'][]['AllowRoot'] = true;<br />#g

and run the command using sed -f like this:

sed -i -f allow_root_true.sed input.txt

Warning -i will change the input file

toftis
  • 1,070
  • 9
  • 26
0

sed can't do literal string matching which is why you need to escape so many characters (see Is it possible to escape regex metacharacters reliably with sed), but awk can:

$ awk -v str="\$cfg['Servers'][\$i]['AllowRoot']" 'index($0,str){sub(/false/,"true")} 1' file
    //some text here
    $cfg['Servers'][$i]['AllowRoot'] = true;<br />
    //some more text here
 Run code snippetHide resultsExpand snippet

In the above we only have to escape the $s to protect them from the shell since the string is enclosed in "s to allow it to include 's.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185