BackupPC-users

[BackupPC-users] OT: (e.g.) sed command to modify configuration file

2009-09-25 15:46:04
Subject: [BackupPC-users] OT: (e.g.) sed command to modify configuration file
From: Timothy J Massey <tmassey AT obscorp DOT com>
To: "General list for user discussion, questions and support" <backuppc-users AT lists.sourceforge DOT net>
Date: Fri, 25 Sep 2009 15:48:53 -0400
Hello!

I have a shell script that I use to install BackupPC.  It takes a standard 
CentOS installation and performs the configuration that I would normally 
do to install BackupPC.  There are probably way better ways of doing this, 
but this is the way I've chosen.

As part of this script, I use sed to modify certain configuration files. 
My sed-fu is weak, however, and I've only gotten it to do the most basic 
things:  insert static text immediately after a simple string match.  For 
example, something like this:

sed -i.org 's/^[ #]*PermitRootLogin *.*$/#&\nPermitRootLogin no/' 
/etc/ssh/sshd_config

What I'm trying to do is search a configuration file for zero or more 
occurrences of a particular configuration element (either commented out or 
not), prepend a # to all of them (again, commented out or not), and append 
the proper configuration line.  The line above works under extremely 
narrow circumstances, but it's very fragile.  Does anyone have a good way 
to do this (sed or otherwise) from within a (bash) shell script?

That's my question.  If you already know the answer, then stop reading 
here and e-mail me the solution!  :)  Otherwise, here's an example of what 
I'm looking for:

Here is a sample configuration file simplified from sshd_config:

#Example of a greatly reduced sshd_config
#Protocol 2,1
Protocol 2
#Additional lines here
#PermitRootLogin yes
#Additional lines here


I want to alter this in two ways:
1) comment out all Protocol lines and add a line "Protocol 2".  (Yes, I 
know it already says this.  Pretend that I want Protocol 1, if it helps.)
2) comment out all PermitRootLogin lines and add a "PermitRootLogin no"

In the end, I'd like to see this:

#Example of a greatly reduced sshd_config
##Protocol 2,1
#Protocol 2
Protocol 2
#Additional lines here
##PermitRootLogin yes
PermitRootLogin no
#Additional lines here

With the sed line I've outlined at the top, it will add a # to the 
beginning of *every* e.g. PermitRootLogin line and add the proper line 
right below that.  It only works right now because there's only one 
PermitRootLogin line.  But it falls down terribly if there are more than 
one, such as with the Protocol line.  AFAICT, there's no way to tell sed 
to either add text only at the last match (which I can understand, it's 
hard to know if it will be the last match until the end, and by then it's 
too late), or to stop editing after the first match and merely dump the 
rest of the file into the output.  Without being able to do either of 
these things, I'm stuck...

Ideas?

Tim Massey


------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
BackupPC-users mailing list
BackupPC-users AT lists.sourceforge DOT net
List:    https://lists.sourceforge.net/lists/listinfo/backuppc-users
Wiki:    http://backuppc.wiki.sourceforge.net
Project: http://backuppc.sourceforge.net/