$_ CLI In Depth
Learning progress 0%
Build · Functions, Arrays & Parameter Expansion

Build: An INI Config File Parser

Write the parser, then test it on a file with comments, spaces, equals signs and no final newline.

~30 min Advanced 2 checked steps

Build it on a real Linux machine

The guided lab boots Linux in your browser. Each step runs a check script on that machine, so it passes only when your files and output are right.

What you will build

Step 1: Parse a simple INI file

The file app.ini is already in /workspace. Write ini.sh, a script that takes a file name and prints one section.key=value line for each setting. Use the reference solution in the build unit as your guide and write the script with a heredoc. Then run bash ini.sh app.ini and compare the output to the spec.

The three lines should be server.host=127.0.0.1, server.port=8080 and db.name=app data.

Step 2: Handle messy input

Run your ini.sh on messy.ini, which is in /workspace. It has an indented comment, spaces around keys and values, a value that contains an equals sign, a line without an equals sign, and a final line with no newline after it.

The output must be web.name=site, web.path=/a=b and empty.last=final, in that order. Lines without an equals sign must be skipped, not printed.

Files

  • walkthrough.sh: every step of the reference solution, in order.

How it is checked

Each step in the guided lab has a check script that runs on the lab machine after you finish. It passes only when the files and output it expects are there, so the lab cannot be completed by reading alone.

Reference files

Read these after you have tried the lab. They are the solutions the checks were verified against.

walkthrough.sh

BASH62 lines
#!/usr/bin/env bash
# Reference solution for every step of the guided lab, in order.

# Step 1: Parse a simple INI file
# (the lab prepares this first)
# cat > /workspace/app.ini <<&#039;EOF&#039;
# # sample
# [server]
# host = 127.0.0.1
# port=8080
# ; comment
# [db]
# name = app data
# EOF
cat > /workspace/ini.sh <<&#039;EOF&#039;
#!/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
    &#039;&#039;|&#039;#&#039;*|&#039;;&#039;*) 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"
EOF

# Step 2: Handle messy input
# (the lab prepares this first)
# printf &#039;  ; indented comment\n[web]\n  name   =   site   \npath=/a=b\n[empty]\nnoequals line\nlast = final&#039; > /workspace/messy.ini
cat > /workspace/ini.sh <<&#039;EOF&#039;
#!/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
    &#039;&#039;|&#039;#&#039;*|&#039;;&#039;*) 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"
EOF

Lessons this build uses