0
0
Bash Scriptingscripting~10 mins

Exit codes ($?) 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 print the exit code of the last command.

Bash Scripting
echo "Hello"; echo [1]
Drag options to blanks, or click blank then click option'
A$?
B$$
C$!
D$#
Attempts:
3 left
💡 Hint
Common Mistakes
Using $$ which gives the process ID instead of the exit code.
Using $! which gives the PID of the last background command.
Using $# which gives the number of positional parameters.
2fill in blank
medium

Complete the code to check if the last command succeeded (exit code 0).

Bash Scripting
some_command
if [ [1] -eq 0 ]; then echo "Success"; fi
Drag options to blanks, or click blank then click option'
A$$
B$?
C$!
D$#
Attempts:
3 left
💡 Hint
Common Mistakes
Using $$ which is the process ID, not the exit code.
Using $! which is the PID of the last background command.
Using $# which is the number of arguments.
3fill in blank
hard

Fix the error in the code to correctly capture the exit code of a command.

Bash Scripting
ls /nonexistent_directory
exit_code=[1]
echo "Exit code: $exit_code"
Drag options to blanks, or click blank then click option'
A$?
B$#
C$!
D$$
Attempts:
3 left
💡 Hint
Common Mistakes
Using $$ which is the process ID, not the exit code.
Using $! which is the PID of the last background command.
Using $# which is the number of arguments.
4fill in blank
hard

Fill both blanks to create a script that runs a command and prints if it failed.

Bash Scripting
command_to_run
if [ [1] -ne 0 ]; then
  echo "Command failed with exit code [2]"
fi
Drag options to blanks, or click blank then click option'
A$?
B$$
D$#
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for the exit code in condition and echo.
Using $$ or $# which are not exit codes.
5fill in blank
hard

Fill all three blanks to save the exit code, check it, and print a message.

Bash Scripting
some_command
exit_code=[1]
if [ [2] -eq 0 ]; then
  echo "Command succeeded with code [3]"
fi
Drag options to blanks, or click blank then click option'
A$?
Bexit_code
D$$
Attempts:
3 left
💡 Hint
Common Mistakes
Using $? directly in the if after assigning it to a variable.
Using $$ which is the process ID.