📄 editconf.perl
字号:
#!/usr/bin/perl -wuse strict;# $Id: editconf.perl,v 1.1 2000/05/19 22:44:34 rusty Exp $## This edits configuration files during software install/uninstall.# Run with --help or see the text of the usage subroutine below for# more information.## Examples:## Add fam to /etc/inetd.conf during install:# editconf inetd.conf add '\bfam\b' \# "# fam, the File Alteration Monitor" \# "sgi_fam/1-2 stream rpc/tcp wait root /usr/local/bin/fam fam"## Remove fam from /etc/inetd.conf during uninstall:# editconf inetd.conf remove '\bfam\b'## Add /usr/sysadm/lib to /etc/ld.so.conf## editconf ld.so.conf add '\bsysadm\b' /usr/sysadm/lib## Add sysadmd to /usr/local/etc/tcpmux.conf:# editconf tcpmux.conf add '\bsysadm\b' \# "# sysadmd, for system administration applications" \# "sgi_sysadm root /usr/sysadm/bin/sysadmd sysadmd"## The main goal of this script is to "do no harm." We don't# modify any files if it looks like we're going to mess with# lines which have been edited by the user. The only time we# change a file is when we're confident that we're adding new# lines, or messing with lines which we ourselves added or# removed during a previous invocation of the script.## Well... that's not entirely true; the "remove" operation comments# out any line that matches the given regexp. But it makes a backup!## USING EDITCONF WITH AUTOMAKE AND RPM## This prepends DESTDIR to the files it operates on (unless you pass# it a file starting with \.{0,2}/, so it can be used during an# automake "make install" into a non-root directory (as you would do# while building an RPM package). Note that the file it attempts to# operate on probably won't be present, though, so you'll probably# need to ignore errors during the make install:## make-install-hook:# -$(EDITCONF) ld.so.conf add '\bsysadm\b' /usr/sysadm/lib## This will probably fail when DESTDIR is set, as ld.so.conf probably# doesn't exist under DESTDIR; the only reason to have this line in the# Makefile.am is so that a normal "make install" will update the# configuration files.## In order to have your configuration files updated during the install/# uninstall of an rpm package, you'll need to add something like this# to your spec file:## # this is %preun rather than %postun because we want to use our# # script before it gets uninstalled.# %preun# perl /usr/local/lib/fam/editconf.perl ld.so.conf remove '\bsysadm\b'##sub usage { my($msg) = @_; $msg && ($msg ne "help") && print STDERR "$msg\n\n"; print STDERR <<"EOF";Usage: $0 [options] file \"add\" regexp lines... $0 [options] file \"remove\" regexp [comment] $0 --helpOptions: -n No-exec (don't change any files) -v Verbose -s Silent -- End argument processing (in case your new config file lines start with -)EOF if($msg eq "help") { print STDERR <<"EOF";This edits configuration files. Given a file name, it looksin a list of directories (see below) for the file. (If the filename starts with "/" or ".", the path list is not searched.)Once the file is found, the given regular expression is searchedfor in the file to determine whether the option or service we'readding/removing already exists.If we're adding new lines to the file, there are four possibleoutcomes: - If the regexp isn't found in the file, we figure our lines haven't been added before, and we add them. - If the regexp is found in the file, and indeed the exact lines we were going to add are already there, we're happy, and we don't change the file. - If the regexp is found in the file, and the lines we were going to add are present but commented out, we uncomment them. - If the regexp is found in the file, but in lines which are different than the lines we were going to add, we figure the option or service we were going to add has already been configured differently; in this case, we make our changes in a new copy of the file and print a warning message saying that someone should compare the two files. (We don't change the configuration of the system in this case.) If we're removing lines from the file, there are two possibleoutcomes: - If the regexp isn't found in the file, we're happy, and we don't change the file. - If the regexp is found in the file, we make a backup of the file, and comment out the lines containing the regexp in the original file.EOF print STDERR "Configuration file paths:\n"; foreach (@::paths) { print STDERR " $_\n"; } } exit 1;}# See if DESTDIR is set, to have us operate on files not in /my $DESTDIR = $ENV{'DESTDIR'} ? $ENV{'DESTDIR'} : "";# This is the list of places we'll look for the configuration file# if we weren't given an absolute path.@::paths = ("$DESTDIR/etc", "$DESTDIR/usr/etc", "$DESTDIR/usr/local/etc");my $comment = '#';my $verbose = &splicegrep('^-v$', \@ARGV, '^--$');my $noexec = &splicegrep('^-n$', \@ARGV, '^--$');my $silent = &splicegrep('^-s$', \@ARGV, '^--$');&splicegrep('^--?h', \@ARGV, '^--$') && &usage("help");&splicegrep('^-', \@ARGV, '^--$') && &usage();&splicegrep('^--$', \@ARGV);my $file; # the name of the file passed on the command linemy $regexp; # the pattern passed on the command linemy $op; # the operation being performed (add|remove)($file = shift) || &usage("The config file name is required!");(($op = shift) && ($op =~ /^(add|remove)$/)) || &usage("\"add\" or \"remove\" is required!");($regexp = shift) || &usage("The regexp to search for is required!");my @lines = @ARGV;## Does the file name start with /, ./, or ../?#if ($file =~ m#^\.{0,2}/#) { # Danger! Not applying $DESTDIR to $path here! &shaketh_thy_booty($op, $file, $regexp, @lines); exit 0;}## No, so we'll search for the file name in the list of paths.#$verbose && $DESTDIR && print "Using DESTDIR \"$DESTDIR\"\n";my($p, $path);foreach $p (@::paths) { $path = "$p/$file"; $verbose && print STDERR "Looking for $path...\n"; if (-f $path) { &shaketh_thy_booty($op, $path, $regexp, @lines); exit 0; }}die("Couldn't find $file in " . join(" ", @::paths) . "\n");## Once we know what file we're attacking, this does the actual work.#sub shaketh_thy_booty { # or is it "thine"? my($op, $path, $regexp, @lines) = @_; $noexec || -w $path || die("I don't have write permission on $path!\n"); # Might as well snort it into memory. Hopefully it's a small file, ha ha. open(CFG, "<$path") || die("Couldn't open $path for input!\n"); my @wholefile = <CFG>; close(CFG); my $matched = 0; if (!grep /$regexp/, @wholefile) { if ($op eq 'add') { # It doesn't contain our regexp, so append our lines and # exit happily. if ($noexec) { print "I would have added the following lines to $path:\n"; foreach (@lines) { print "$_\n"; } } else { open(CFG, ">>$path") || die("Couldn't open $path for append!\n"); $silent || print "Added the following lines to $path:\n"; foreach (@lines) { $silent || print "$_\n"; print CFG "$_\n"; } $silent || print "(end of lines added to $path)\n"; } exit 0; } elsif ($op eq 'remove') { # It doesn't contain our regexp, so we don't need to remove it, # so we're happy. exit 0; } else { die("bad op \"$op\""); } } if ($op eq "remove") { # Since we're still here, and we're removing this entry, comment out # all lines matching our regexp. if ($noexec) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -