Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to stop the script if a command fails.
Bash Scripting
set [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set -x' which only prints commands but doesn't stop on errors.
Forgetting to use any option, so errors are ignored.
✗ Incorrect
Using 'set -e' makes the script exit immediately if any command returns a non-zero status, preventing silent failures.
2fill in blank
mediumComplete the code to check if the last command failed and print an error message.
Bash Scripting
if [[ [1] -ne 0 ]]; then echo "Error occurred"; fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '$#' which counts script arguments, not exit status.
Using '$$' which is the process ID, not exit status.
✗ Incorrect
The special variable $? holds the exit status of the last command; non-zero means failure.
3fill in blank
hardFix the error in the code to correctly handle command failure.
Bash Scripting
command_that_might_fail if [ [1] ]; then echo "Failed"; fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for success instead of failure.
Using '==' inside single brackets which is invalid.
✗ Incorrect
To detect failure, check if $? is not equal to 0 using '-ne 0'.
4fill in blank
hardFill both blanks to run a command and exit if it fails.
Bash Scripting
[1] || [2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'continue' which is invalid outside loops.
Not exiting on failure, so errors are ignored.
✗ Incorrect
The '||' operator runs the right command if the left fails; here, exit 1 stops the script on failure.
5fill in blank
hardFill all three blanks to create a function that runs a command and handles errors.
Bash Scripting
run_command() {
[1]
if [[ [2] -ne 0 ]]; then
[3]
fi
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not passing arguments to the command.
Not checking exit status properly.
Not exiting on failure.
✗ Incorrect
The function runs the command with arguments, checks exit status, and exits with error message if failed.