-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfset
More file actions
118 lines (102 loc) · 3.46 KB
/
confset
File metadata and controls
118 lines (102 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/perl
# (c) by ecm4u GmbH
# Author Heiko Robert <heiko.robert@ecm4u.de>
# this script is part of the ecm4u virtual appliance for Alfresco
# set parameters in config files
# run without parameters to see help
# adopted from http://unix.stackexchange.com/questions/139574/change-a-value-in-a-config-file-or-add-the-setting-if-it-doesnt-exist
# author: Stephen Ostermiller
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);
}