Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to run a script and check its exit status.
Bash Scripting
bash [1]; echo $? # Prints the exit status of the script
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a Python script name instead of a bash script.
Forgetting to specify the script file name.
✗ Incorrect
The command 'bash myscript.sh' runs the script named 'myscript.sh'.
2fill in blank
mediumComplete the code to test if a variable is empty in a bash script.
Bash Scripting
if [ -z [1] ]; then echo "Empty"; fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using the $ sign before the variable name.
Using quotes incorrectly around the variable.
✗ Incorrect
The syntax '-z $var' checks if the variable 'var' is empty.
3fill in blank
hardFix the error in the code to correctly test if a file exists.
Bash Scripting
if [[ -f [1] ]]; then echo "File exists"; fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the $ sign before the variable.
Using single brackets [ ] with variable without quotes.
✗ Incorrect
Use '$filename' to reference the variable holding the file name.
4fill in blank
hardFill both blanks to create a loop that tests each file in a directory.
Bash Scripting
for file in [1]; do if [[ -f [2] ]]; then echo "$file exists"; fi; done
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the '*' wildcard.
Using 'file' without $ to reference the variable.
✗ Incorrect
The loop iterates over all files with '*', and tests each with '$file'.
5fill in blank
hardFill all three blanks to create a function that tests if input is a directory and prints a message.
Bash Scripting
check_dir() {
if [[ -d [1] ]]; then
echo "[2] is a directory"
else
echo "Not a directory"
fi
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the variable, causing errors with spaces.
Using variable names instead of positional parameters.
✗ Incorrect
Use "$1" to safely reference the first argument as a directory path and print it.