简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM