Interview Prep/Linux/Scheduling, automation and scripting

A script must read a config file with `KEY=VALUE` lines, but you don't want to `source` it because it could contain commands. What do you do?

AdvancedScenarioScheduling, automation and scripting
Answer

Situation read → Parse instead of executing.

What I'd do →

declare -A cfg
while IFS='=' read -r key value; do
  [[ "$key" =~ ^[A-Z_][A-Z0-9_]*$ ]] || continue
  cfg["$key"]="$value"
done < /etc/myapp/settings.conf
echo "${cfg[DB_HOST]}"

Why → source runs whatever is in the file with the script's privileges, which is dangerous if the file is writable by others.

Interview tip → Mention checking the file owner and permissions before trusting it.

All Linux questions Roadmap: Linux

More on Scheduling, automation and scripting