Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question

3 Answers 3

up vote 2 down vote accepted

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
share|improve this answer

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.

share|improve this answer

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.

share|improve this answer
    
While ex is powerful, it's difficult to recover from errors (e.g. when an expected line is missing). Here you can get away without any conditional behavior, but this doesn't generalize so well. –  Gilles Jun 3 '13 at 18:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.