01 / INVENTORY AND CONFIGURATION · LESSON 04

Merge reviewed configuration

Apply explicit configuration precedence while preserving values as inert text.

16 min · learn at your pace

Precedence is a policy

A base configuration can supply defaults while a site file supplies local choices. State which layer wins before writing the merge. In this exercise site values override base values; within either layer the last occurrence of a key wins. An empty value is an explicit override rather than a missing setting.

The format is restricted: valid identifier keys, an equals sign, and the remainder of the line as the value. Blank lines and lines starting with a hash are ignored. Values can contain spaces, percent signs and additional equals signs. Split at the first equals sign, not at every separator.

Configuration stays data

Sourcing these files would execute shell substitutions and would give spaces and punctuation another meaning. Instead, read each line with a fixed awk program. No value is executed or interpreted as a printf format. This is a small teaching format, not a universal parser for INI, shell files or application-specific configuration.

Carry the winning source layer into the report. That provenance answers the practical question “why did this machine get this value?” Sort final keys under LC_ALL=C for a stable comparison. A later lesson adds strict policy validation before a script is allowed to make a virtual-file change.

Try an example.sh
awk 'index($0,"=") {at=index($0,"=");printf "key=%s value=%s\n",substr($0,1,at-1),substr($0,at+1)}' base.conf
YOUR TURN

Put it into practice.

Merge base.conf then site.conf. Inputs contain valid identifier keys followed by = and a value with no tabs/newlines; ignore empty lines and lines beginning #. The last occurrence wins within a file, and site overrides base, including an empty value. Values may contain spaces, Unicode or more equals signs and must never execute. Print key<TAB>value<TAB>source sorted by key, with source base or site.

See an explained solution
One way to solve it.sh
export LC_ALL=C
printf 'key\tvalue\tsource\n'
awk '/^#/ || $0=="" {next}
{at=index($0,"=");key=substr($0,1,at-1);v[key]=substr($0,at+1);source[key]=(FILENAME==ARGV[1]?"base":"site")}
END {for(key in v) printf "%s\t%s\t%s\n",key,v[key],source[key]}
' base.conf site.conf | sort

The merge reads values literally and records which layer supplied the winning assignment. Using the first equals sign preserves values such as connection labels, while membership is independent of whether a value is empty.