Challenge - 5 Problems
Config File Reading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Reading a simple config file with key-value pairs
Given a config file
What is the output of this script?
settings.conf with the content:username=admin password=secret port=8080
What is the output of this script?
while IFS='=' read -r key value; do echo "$key -> $value" done < settings.conf
Bash Scripting
while IFS='=' read -r key value; do echo "$key -> $value" done < settings.conf
Attempts:
2 left
💡 Hint
Think about how the read command splits lines using the IFS variable.
✗ Incorrect
The script reads each line splitting at '=' into key and value, then prints them with an arrow.
💻 Command Output
intermediate2:00remaining
Ignoring comments and blank lines in config file
Consider a config file
What does this script output?
app.conf with:# This is a comment host=localhost # Another comment port=3306
What does this script output?
grep -v '^#' app.conf | grep -v '^$' | while IFS='=' read -r key value; do echo "$key=$value" done
Bash Scripting
grep -v '^#' app.conf | grep -v '^$' | while IFS='=' read -r key value; do echo "$key=$value" done
Attempts:
2 left
💡 Hint
grep -v '^#' removes comment lines, grep -v '^$' removes blank lines.
✗ Incorrect
The script filters out comments and blank lines, then reads key-value pairs and prints them.
📝 Syntax
advanced2:00remaining
Parsing config with spaces around equals sign
Which script correctly reads a config file where lines may have spaces around '=' like:
and prints key and value?
user = admin password = secret
and prints key and value?
Attempts:
2 left
💡 Hint
Think about how to trim spaces around keys and values.
✗ Incorrect
Option D uses string manipulation to split at '=' and preserves spaces to trim later if needed.
🔧 Debug
advanced2:00remaining
Why does this config reading script fail?
This script tries to read
But it prints nothing. What is the problem?
config.cfg and print keys and values:while IFS='=' read -r key value do echo "$key: $value" done < config.cfg
But it prints nothing. What is the problem?
Bash Scripting
while IFS='=' read -r key value do echo "$key: $value" done < config.cfg
Attempts:
2 left
💡 Hint
Check the syntax of the while loop and read command carefully.
✗ Incorrect
The read command line must end with a semicolon or newline before 'do'. Without it, the shell does not parse the loop correctly.
🚀 Application
expert3:00remaining
Extracting config values into variables
Given a config file
Which script correctly reads these values and sets bash variables
settings.ini with:host=127.0.0.1 port=8080 user=guest
Which script correctly reads these values and sets bash variables
host, port, and user accordingly?Attempts:
2 left
💡 Hint
Think about how to safely assign variables from a file in bash.
✗ Incorrect
Option C reads each line, splits key and value, and exports them as variables. Option C fails because the file is not a valid bash script (no spaces allowed). Option C declares variables but does not export them. Option C uses eval which can be unsafe.