The specification
Your parser reads one file, given as its first argument, and prints one line for each key. The output format is section.key=value. The rules are short. Lines that are blank, or whose first non-space character is a hash or a semicolon, are comments and are skipped. A line of the form [name] starts a section. A line with an equals sign is a key and value pair, and spaces around the key and the value are removed. Everything after the first equals sign belongs to the value, so values may contain spaces and more equals signs. Keys that appear before any section have no prefix.
Input:
# sample
[server]
host = 127.0.0.1
port=8080
; comment
[db]
name = app data
Output:
server.host=127.0.0.1
server.port=8080
db.name=app dataDesign decisions
Read the file with a while IFS= read -r line loop. The IFS= keeps leading whitespace, and -r keeps backslashes as they are. The loop must also handle a last line that has no trailing newline, which read reports as a failure even though it stored the text. Testing the line with || [[ -n $line ]] handles that case.
Trimming uses parameter expansion from the trimming unit, not sed, so the script starts no extra processes for each line. Removing leading whitespace means deleting everything up to the first non-space character, and removing trailing whitespace means deleting everything from the last non-space character to the end.
The reference solution
The solution below follows the specification. Read it line by line, and check how each piece of the spec maps to code. It is one possible design, and it is kept short on purpose.
#!/usr/bin/env bash
# Usage: ini.sh FILE
# Prints one section.key=value line for every key in FILE.
set -euo pipefail
file=${1:?usage: ini.sh FILE}
section=
while IFS= read -r line || [[ -n $line ]]; do
line=${line#"${line%%[![:space:]]*}"}
line=${line%"${line##*[![:space:]]}"}
case $line in
''|'#'*|';'*) continue ;;
\[*\]) section=${line:1:-1}; continue ;;
esac
[[ $line == *=* ]] || continue
key=${line%%=*}
value=${line#*=}
key=${key%"${key##*[![:space:]]}"}
value=${value#"${value%%[![:space:]]*}"}
printf "%s.%s=%s\n" "$section" "$key" "$value"
done < "$file"The first part trims the line in place using two expansions, one for the leading whitespace and one for the trailing. The case statement skips comments and blank lines, and it recognizes a section header by its brackets. The section name is the text between the first and last character, which the substring form ${line:1:-1} extracts. A line without an equals sign is skipped rather than printed as a broken key.
Why the output is flat
A flat format is easy to use with the tools from earlier modules. A caller can find one value with grep "^db.name=", or can cut a section with grep "^server\.". Choosing a line format rather than a nested structure keeps the parser small and makes its output easy to test with a diff.
Testing the parser
Test the parser against an input that has every feature in the spec, and against a file with edge cases such as a value containing an equals sign, a key with spaces around it, and a final line without a newline. The lab for this build asks you to write the parser and check it against two files. Use the output of the reference solution to compare your own output, and look for the first line that differs.
Extensions to try
Once the basic parser works, add a second argument that prints only one section, or make the parser exit with a message when a line has no section and no equals sign. Each extension is a few lines and reinforces one unit of this module, so treat the build as a place to combine what you have learned.