saji89
saji89

Reputation: 2251

How to modify configuration lines using shell scripting

I have a phppgadmin configuration file (/usr/share/phppgadmin/conf/config.inc.php) It has a line:

$conf['extra_login_security'] = false;

I would like to change it to:

$conf['extra_login_security'] = true;

using bash shell-script. I understand that I would have to use something like sed/awk. But don't know how exactly to use it to do what I want.

Upvotes: 1

Views: 1152

Answers (1)

Chris Seymour
Chris Seymour

Reputation: 85913

If you want a simple global substitution of false to true then sed 's/false/true/g' will do it.

$ echo "conf['extra_login_security'] = false" | sed 's/false/true/g'
conf['extra_login_security'] = true

However to only change lines the start with conf[ and just change the value to true then this is better sed 's/\(^.*conf\[.*\] =\) false/\1 true/g'

echo "conf['extra_login_security'] = false" | sed 's/\(^.*conf\[.*\] =\) false/\1 true/g'
conf['extra_login_security'] = true

EDIT:

Just that line can be done with the following:

sed -i 's/\(^.*conf\[.extra_login_security.\] =\) false/\1 true/' /usr/share/phppgadmin/conf/config.inc.php

Use -i to save the changes to the file, or redirect to a new file if you don't want to overwrite the original.

sed 's/\(^.*conf\[.extra_login_security.\] =\) false/\1 true/' > mynewfile.php

A trick if you don't have permission to write mynewfile.php is to pipe the output to tee and sudo that:

sed 's/\(^.*conf\[.extra_login_security.\] =\) false/\1 true/' | sudo tee mynewfile.php

From man tee tee - read from standard input and write to standard output and files.

Upvotes: 2

Related Questions