BackupPC-users

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

2009-09-25 17:23:10
Subject: Re: [BackupPC-users] OT: (e.g.) sed command to modify configuration file
From: Davide Brini <dave_br AT gmx DOT com>
To: backuppc-users AT lists.sourceforge DOT net
Date: Fri, 25 Sep 2009 22:13:29 +0100
On Friday 25 September 2009, Timothy J Massey wrote:
> 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...

What you can do is to blindly comment out all lines you don't want, and when 
you're at the last line, add your lines. Something like this:

sed 's/^Protocol/#&/;$s/$/\nProtocol 2/'

(note that the above assumes you're using a sed that recognizes \n in 
replacements, line GNU sed)

This comments out all lines starting with "Protocol", and appends "Protocol 2" 
after the very last line.

It can be extended to comment out different types of lines, for example 
suppose you want to add three lines:

Protocol 2
PermitRootLogin no
PrintLastLog yes

So you can do something like this (put this in a file, and run it with sed -f 
program.sed):

s/^Protocol/#&/
s/^PermitRootLogin/#&/
s/^PrintLastLog/#&/
$s/$/\nProtocol 2\nPermitRootLogin no\nPrintLastLog yes/

If you're using GNU sed as it's likely since you mention CentOS, you can 
exploit its extended regex capabilities and do, in a more compact way:

# invoke with sed -r
s/^(Protocol|PermitRootLogin|PrintLastLog)/#&/
$s/$/\nProtocol 2\nPermitRootLogin no\nPrintLastLog yes/

Keep in mind, though, that at some point (for example if you require some 
other complex modification/customization) the problem becomes too complicated 
to be easily solved with tools like sed alone.

Hope that helps.

-- 
D.

------------------------------------------------------------------------------
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/