HANDS-ON TASK · 1 OF 1

Config generator with validation

Project: Config generator with validation

You're a DevOps engineer at esc bash. Deploys keep breaking because someone hand-edits the app's config file and forgets a required value. Write a generator that builds the config from environment variables, fills sensible defaults for the optional settings, and refuses to run when a required value is missing.

Set up the project

Run this first. It prepares the machine for the task:

bash
curl -fsSL https://raw.githubusercontent.com/Esc-Bash/project-init-scripts/main/shell-scripting/project-2/init.sh | bash

This creates the /root/app/ folder where your config will be written.

What to build

Write /root/scripts/genconfig.sh that takes one argument: the path of the config file to write.

genconfig.sh <output-path>

Start the script with set -euo pipefail. It reads four values from the environment:

  • APP_NAME - required.
  • APP_PORT - required.
  • APP_ENV - optional, defaults to production.
  • LOG_LEVEL - optional, defaults to info.

Behavior:

  1. If either required value is missing, print an error to stderr and exit non-zero. Do not write a config in that case.

  2. Otherwise write the output file with exactly one KEY=value line per setting, in this format:

    APP_NAME=web
    APP_PORT=8080
    APP_ENV=production
    LOG_LEVEL=info
    

For example, this call should produce the four lines above with the defaults filled in:

APP_NAME=web APP_PORT=8080 genconfig.sh /root/app/config.env

When the checks pass, press Submit.

Start the lab on the right to run checks.

Pass the checks to continue
Spin up a fresh environment and practice live.
linux-devops-basic · fresh machine · ready in under a minute