Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to safely check if a file exists before reading it.
Bash Scripting
if [ -f "[1]" ]; then cat "[1]" fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name without $ so it is treated as a string.
Not quoting the variable which can cause errors if it contains spaces.
✗ Incorrect
Using "$filename" ensures the variable is expanded safely and correctly in the test and command.
2fill in blank
mediumComplete the code to prevent running commands from untrusted input.
Bash Scripting
safe_var=$(printf '%q' [1]) echo $safe_var
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using $ so the variable is treated as a literal string.
Adding extra quotes inside the command causing double quoting.
✗ Incorrect
Using "$user_input" with $ expands the variable safely. printf '%q' escapes special characters to prevent code injection.
3fill in blank
hardFix the error in the code to safely read a filename from user input.
Bash Scripting
read -r filename if [[ -n [1] ]]; then cat "$filename" fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking the variable name as a string instead of its value.
Using quotes incorrectly inside the test.
✗ Incorrect
Using "$filename" with $ inside [[ ]] checks if the variable is not empty safely.
4fill in blank
hardFill both blanks to safely loop over filenames with spaces.
Bash Scripting
for file in [1]; do echo "Processing: $[2]" done
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted variables causing word splitting.
Using wrong variable names inside the loop.
✗ Incorrect
Using "${files[@]}" expands each element safely preserving spaces. The loop variable is 'file' to access each filename.
5fill in blank
hardFill all three blanks to create a secure temporary file and write safely.
Bash Scripting
tmpfile=$(mktemp [1]) echo "Data" > [2] chmod [3] "$tmpfile"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using mktemp options causing insecure temp files.
Writing to wrong file or not using variable.
Setting wrong permissions making file accessible to others.
✗ Incorrect
mktemp with -t creates a temp file with a template. Writing to $tmpfile saves data there. chmod 600 restricts file access to owner only.