0
0
Bash Scriptingscripting~20 mins

Configuration file reading in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Config File Reading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Reading a simple config file with key-value pairs
Given a config file 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
A
username -&gt; admin
password -&gt; secret
port -&gt; 8080
B
username
admin
password
secret
port
8080
C
username=admin
password=secret
port=8080
DSyntaxError: invalid syntax
Attempts:
2 left
💡 Hint
Think about how the read command splits lines using the IFS variable.
💻 Command Output
intermediate
2:00remaining
Ignoring comments and blank lines in config file
Consider a config file 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
A
host=localhost
port=3306
B
# This is a comment
host=localhost

# Another comment
port=3306
C
host
localhost
port
3306
DError: command not found
Attempts:
2 left
💡 Hint
grep -v '^#' removes comment lines, grep -v '^$' removes blank lines.
📝 Syntax
advanced
2:00remaining
Parsing config with spaces around equals sign
Which script correctly reads a config file where lines may have spaces around '=' like:
user = admin
password = secret

and prints key and value?
Awhile IFS='=' read key value; do echo "$key -> $value"; done < config.txt
Bwhile IFS='=' read -r key value; do echo "$key -> $value"; done < config.txt
Cwhile IFS=' = ' read -r key value; do echo "$key -> $value"; done < config.txt
Dwhile read -r line; do key=${line%%=*}; value=${line#*=}; echo "$key -> $value"; done < config.txt
Attempts:
2 left
💡 Hint
Think about how to trim spaces around keys and values.
🔧 Debug
advanced
2:00remaining
Why does this config reading script fail?
This script tries to read 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
AFile config.cfg is empty
BMissing semicolon or newline after read command causes syntax error
CIFS should be set to ':=' instead of '='
Dread command needs -a option to read arrays
Attempts:
2 left
💡 Hint
Check the syntax of the while loop and read command carefully.
🚀 Application
expert
3:00remaining
Extracting config values into variables
Given a config file 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?
Awhile IFS='=' read -r key value; do declare "$key=$value"; done < settings.ini
Bsource settings.ini
Cwhile IFS='=' read -r key value; do export $key=$value; done < settings.ini
Deval $(sed 's/^/export /' settings.ini)
Attempts:
2 left
💡 Hint
Think about how to safely assign variables from a file in bash.