从命令行修改配置文件时,我经常想在配置文件中查找设置并修改该行(如果该设置存在)。如果该设置不存在,我想将其添加到文件末尾。
我最终做了类似的事情:
if [ `grep -c '^setting=' example.conf` == 0 ]
then
echo "setting=value" >> example.conf
else
sed -i 's/^setting=.*/setting=value/g' example.conf
fi
对于如此简单的事情来说,这似乎是一个非常多的代码。这甚至没有做一些基本的事情,比如在附加到配置文件之前检查配置文件是否已经以新行结束。当然有一个实用程序可以执行此操作,或者我可以使用一个更简单的命令。
答案1
这是我刚刚编写的 confset Perl 脚本,我将把它放在我的路径中:
- 可以在一次调用中处理多个文件
- 可以在一次调用中修改每个文件中的多个配置值
- 可以指定分隔符(用
--separator
) - 对名称周围的空白保持自由的选择
Usage: confset <options> name1=value1 name2=value2 file1.conf file2.conf
Options:
-s --separator <value> What comes between names and values (default =)
-w --whitespace <true|false> Allow space around names and values (default false)
因此,为了处理我在问题中概述的情况,我会这样称呼它:
confset example.conf setting=value
这是脚本:
#!/usr/bin/perl
use strict;
my $scriptname = $0;
my $separator = '=';
my $whitespace = 0;
my @files = ();
my @namevalues = ();
# read in the command line arguments
for (my $i=0; $i<scalar(@ARGV); $i++){
my $arg = @ARGV[$i];
if ($arg =~ /^-/){
&printHelp(*STDOUT, 0) if ($arg eq "-h" or $arg eq "--help");
&printHelp(*STDERR, 1) if ($i+1 >= scalar(@ARGV));
my $opt = @ARGV[++$i];
if ($arg eq "-s" or $arg eq "--separator"){
$separator = $opt;
} elsif ($arg eq "-w" or $arg eq "--whitespace"){
$whitespace = 0;
$whitespace = 1 if ($opt =~ /1|t|y/);
} else {
&printHelp(*STDERR, 1);
}
} elsif ( -e $arg){
push(@files, $arg);
} else {
push(@namevalues, $arg);
}
}
# check the validity of the command line arguments
if (scalar(@files) == 0){
print STDERR "ERROR: No files specified\n";
printHelp(*STDERR, 1);
}
if (scalar(@namevalues) == 0){
print STDERR "ERROR: No name value pairs specified\n";
printHelp(*STDERR, 1);
}
my $names = {};
foreach my $namevalue (@namevalues){
my ($name, $value) = &splitnv($namevalue);
if ($name){
$names->{$name} = {"value",$value,"replaced",0};
} else {
print STDERR "ERROR: Argument not a file and contains no separator: $namevalue\n";
printHelp(*STDERR, 1);
}
}
# Do the modification to each conf file
foreach my $file (@files){
# read in the entire file into memory
my $contents = "";
open FILE, $file or die $!;
while (my $line = <FILE>){
chomp $line;
my ($name, $value) = &splitnv($line);
# set matching lines to their new value
if ($names->{$name}){
$line = $name . $separator . $names->{$name}->{value};
$names->{$name}->{replaced} = 1;
}
$contents .= "$line\n";
}
close FILE or die $!;
# add any new lines that didn't already get set
foreach my $name (keys %$names){
if (!$names->{$name}->{replaced}){
$contents .= $name . $separator . $names->{$name}->{value}."\n";
}
# reset for next file
$names->{$name}->{replaced} = 0;
}
# overwrite the file
open FILE, ">$file" or die $!;
print FILE $contents;
close FILE or die $!;
}
# Print help message to the specified stream and exit with the specified value
sub printHelp(){
my ($stream, $exit) = @_;
print $stream "Usage: $scriptname <options> name1=value1 name2=value2 file1.conf file2.conf\n";
print $stream "Options:\n";
print $stream " -s --separator <value> What comes between names and values (default =)\n";
print $stream " -w --whitespace <true|false> Allow space around names and values (default false)\n";
exit $exit;
}
# Split a string into a name and value using the global separator
sub splitnv(){
my ($str) = @_;
my $ind = index($str, $separator);
return (0,0) if ($ind < 0);
my $name = substr($str, 0, $ind);
my $value = substr($str, $ind+length($separator));
$name =~ s/(^[ \t])*|([ \t])*$//g if ($whitespace);
return ($name, $value);
}
答案2
额外的逻辑可以用 来处理awk
。
BEGIN { FS = OFS = "=" }
$1 == "setting" { $2 = "value"; found=1 }
{print}
END { if (!found) { print "setting=value" }
如果最后未找到该属性,则found
不会设置该属性,并且 END 子句将附加新的配置行。确保FS=OFS=
格式相同,并且打印将始终发送换行符 (ORS),包括最后一行。空行和注释将原封不动地通过。