0
0
Bash Scriptingscripting~15 mins

Custom exit codes (exit N) in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Custom Exit Codes in Bash Scripts
📖 Scenario: You are writing a small bash script to check if a user is allowed to enter a club based on their age. The script will exit with different codes to show if the user is too young, just right, or too old.
🎯 Goal: Build a bash script that uses custom exit codes with exit N to indicate different age categories.
📋 What You'll Learn
Create a variable age with a specific value.
Create a variable min_age to set the minimum allowed age.
Use if statements to check the age and exit with custom codes.
Print a message showing the exit code after running the script.
💡 Why This Matters
🌍 Real World
Scripts often need to tell other programs or users what happened by using exit codes. Custom exit codes help identify specific situations like errors or special cases.
💼 Career
Understanding exit codes is important for automation, debugging, and writing scripts that work well with other tools in system administration and DevOps.
Progress0 / 4 steps
1
Set the user's age
Create a variable called age and set it to 20.
Bash Scripting
Need a hint?

Use age=20 to assign the value.

2
Set the minimum allowed age
Create a variable called min_age and set it to 18.
Bash Scripting
Need a hint?

Use min_age=18 to assign the value.

3
Use if statements to set custom exit codes
Write an if statement that checks if age is less than min_age. If yes, exit with code 1. Otherwise, if age is between min_age and 30 inclusive, exit with code 0. Else, exit with code 2.
Bash Scripting
Need a hint?

Use if, elif, and else with exit N commands.

4
Print the exit code after running the script
Run the script and print the exit code using echo and $?.
Bash Scripting
Need a hint?

After running the script, use echo $? in the terminal to see the exit code.