0
0
Bash Scriptingscripting~10 mins

Configuration file reading in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to read a configuration file line by line.

Bash Scripting
while IFS= read -r [1]; do
  echo "$line"
done < config.txt
Drag options to blanks, or click blank then click option'
Afile
Bconfig
Cline
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not used later in the loop.
Forgetting to use 'read -r' to prevent backslash escapes.
2fill in blank
medium

Complete the code to extract the value of 'username' from the config file.

Bash Scripting
username=$(grep '^username=' config.txt | cut -d'=' -f[1])
Drag options to blanks, or click blank then click option'
A2
B0
C3
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using field 1 which returns the key, not the value.
Using field 0 which is invalid.
3fill in blank
hard

Fix the error in the code to ignore empty lines and comments starting with '#'.

Bash Scripting
while IFS= read -r line; do
  if [[ ! $line =~ ^# ]] && [[ -n $[1] ]]; then
    echo "$line"
  fi
done < config.txt
Drag options to blanks, or click blank then click option'
Afile
Bline
Cconfig
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that was not defined.
Not checking for empty lines properly.
4fill in blank
hard

Fill both blanks to create a dictionary (associative array) from config file lines with key=value format.

Bash Scripting
declare -A config
while IFS='=' read -r [1] [2]; do
  config[$key]=$value
done < config.txt
Drag options to blanks, or click blank then click option'
Akey
Bval
Cvalue
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names that don't match the assignment.
Using the same variable name for both key and value.
5fill in blank
hard

Fill all three blanks to print all keys and values from the associative array.

Bash Scripting
for [1] in "${!config[@]}"; do
  echo "[2] = ${config[[3]]}"
done
Drag options to blanks, or click blank then click option'
Akey
Bconfig
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the loop and inside the echo statement.
Using the array name instead of the key variable.