Editing config file via a bash script

I’ve been trying to write a simple bash script which I’ll be using to install an application and update it’s config file. I’m having hard time to have get it’s config file modified.

# DBHost=localhost
DBName=test
# DBPassword=

any suggestions how I can get above modified as below?

DBHost=localhost
DBName=database
DBPassword=password

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The best way depends on whether you expect the file to be also modified by humans, how complex the file is, and whether you want your script to take precedence if it looks like someone else wants a different value. That is, if the file already contains DBPassword=swordfish, do you want to keep that, or replace it by DBPassword=password?

A common way of dealing with this is to have a section of the file delimited by “magic comments”, and only edit the part between these comments. Here’s a way to do this with awk. If the magic comments are not present, the new section is added at the end of the file. Warning: untested code.

begin_marker='# BEGIN AUTOMATICALLY EDITED PART, DO NOT EDIT'
end_marker='# END AUTOMATICALLY EDITED PART'
new_section='DBHost=localhost
DBName=database
DBPassword=password'
export begin_marker end_marker
awk <file.conf >file.conf.new -v begin_marker="$begin_marker" -v begin_marker="$end_marker" -v new_section="$new_section" '
    1 {print}
    $0 == begin_marker && !changed {
        do {getline} while ($0 != end_marker); # discard old section content
        print new_section;
        print;
        changed = 1;
    }
    END {if (!changed) {print begin_marker; print new_section; print end_marker;}}
'
ln -f file.conf file.conf.old
mv -f file.conf.new file.conf

This approach doesn’t work well if the program that reads the configuration file doesn’t support multiple lines setting the same configuration item. In that case, you’ll really need to remove the old ones. In this case I would advise leaving the comments untouched and adding your own settings at the end.

grep -vE '^[[:blank:]]*(DBHost|DBName|DBPassword)=$' <file.conf >file.conf.new
cat <<EOF >>file.conf.new
DBHost=localhost
DBName=database
DBPassword=password
EOF
ln -f file.conf file.conf.old
mv -f file.conf.new file.conf

Method 2

If that is all you need, you could do something like

sed  -i.bak -e 's/DBName=.*/DBName=database/' 
 -e 's/#* *DBHost=.*/DBHost=localhost/' 
 -e 's/#* *DBPassword=.*/DBPassword=password/' config.file

This will copy the original config.file to config.file.bak and make the necessary changes.

Method 3

I’ll go with “ex” in silent mode:

ex -s $CONFIG_FILE << END_CMDS
%s/^#.DBHost/DBHOST/
%s/DBName=test/DBName=database/
%s/^#.DBPassword/DBPassword/
w!
q
END_CMDS

terdon’s sed command is compact and efficient. But the interaction of a “here document” full of ex commands and shell string interpolation can be powerful. For example, the password could be substituted as the value of a shell variable:

%s/^#.DBPassword=password/DBPassword=$PASSWORD/

If you use vi or vim and you’re familiar with the ‘:’ mode, adding new/better/different edits is also quite easy.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x