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.
More on Scheduling, automation and scripting
A database dump must run every night at 1:30 AM. How do you schedule it with cron so it is reliable?BeginnerConvert that nightly cron job to a systemd timer. Why would you?IntermediateA cron job runs every 5 minutes, but sometimes takes 8 minutes and two copies overlap, corrupting data. How do you prevent that?BeginnerYou have 300 servers running the same cron job at midnight, and they hammer a shared API. How do you fix it?IntermediateA job must run on the last day of every month. Cron has no "last day" field. What do you do?IntermediateYou inherited a server and need to know every scheduled job on it. Where do you look?Beginner