postfwd defaults: keep lines with the wanted value, no reload if unchanged
- Never comment out a line that already has the wanted value, even if a differing setting of the same variable precedes it. - update_postfwd_defaults.sh: leave postfwd alone if nothing changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+20
-9
@@ -120,19 +120,30 @@ set_postfwd_default() {
|
||||
_tmp_file="$(mktemp)"
|
||||
awk -v var="$_var" -v val="$_val" '
|
||||
function unq(s) {
|
||||
gsub(/^[ \t]+|[ \t]+$/, "", s)
|
||||
gsub(/^[ \t\r]+|[ \t\r]+$/, "", s)
|
||||
if (s ~ /^".*"$/ || s ~ /^\047.*\047$/) s = substr(s, 2, length(s) - 2)
|
||||
return s
|
||||
}
|
||||
$0 ~ "^[ \t]*" var "=" {
|
||||
cur = $0 ; sub("^[ \t]*" var "=", "", cur)
|
||||
if (! done && unq(cur) == unq(val)) { print ; done = 1 ; next }
|
||||
print "#" $0
|
||||
if (! done) { print var "=" val ; done = 1 }
|
||||
next
|
||||
function is_set(s) { return s ~ ("^[ \t]*" var "=") }
|
||||
function is_wanted(s) {
|
||||
sub("^[ \t]*" var "=", "", s)
|
||||
return unq(s) == unq(val)
|
||||
}
|
||||
# - Read the whole file first: a line with the wanted value must stay
|
||||
# - untouched, no matter which other settings of VAR precede it.
|
||||
{
|
||||
line[NR] = $0
|
||||
if (is_set($0) && is_wanted($0)) found = 1
|
||||
}
|
||||
END {
|
||||
for (i = 1; i <= NR; i++) {
|
||||
if (! is_set(line[i])) { print line[i] ; continue }
|
||||
if (is_wanted(line[i])) { print line[i] ; continue }
|
||||
print "#" line[i]
|
||||
if (! found) { print var "=" val ; found = 1 }
|
||||
}
|
||||
if (! found) print var "=" val
|
||||
}
|
||||
{ print }
|
||||
END { if (! done) print var "=" val }
|
||||
' "$postfwd_default_file" > "$_tmp_file" 2> "$log_file"
|
||||
|
||||
if [[ $? -ne 0 ]] ; then
|
||||
|
||||
Reference in New Issue
Block a user