Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The variable 'line' is commonly used to store each line read from the file.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using field 1 which returns the key, not the value.
Using field 0 which is invalid.
✗ Incorrect
The value is after the '=' sign, which is the second field when splitting by '='.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that was not defined.
Not checking for empty lines properly.
✗ Incorrect
The condition checks if the variable 'line' is not empty and does not start with '#'.
4fill in blank
hardFill 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'
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.
✗ Incorrect
The first blank is the key variable, the second blank is the value variable.
5fill in blank
hardFill 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'
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.
✗ Incorrect
The loop variable is 'key', used to access keys and values in the array.