0
0
Bash Scriptingscripting~10 mins

Script security best practices 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 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'
A$filename
B/etc/passwd
Cfilename
D$file
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.
2fill in blank
medium

Complete 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'
A'$user_input'
B$user_input
Cuser_input
D"$user_input"
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.
3fill in blank
hard

Fix 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'
A$filename
Bfilename
C"$filename"
D'$filename'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking the variable name as a string instead of its value.
Using quotes incorrectly inside the test.
4fill in blank
hard

Fill 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'
A"${files[@]}"
Bfiles
Cfile
D"$files"
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted variables causing word splitting.
Using wrong variable names inside the loop.
5fill in blank
hard

Fill 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'
A-t tmp.XXXXXX
B$tmpfile
C600
D-d /tmp
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.