Module Title: dgt_bash03 – Command Exit Status
Module Overview:
In this module, you will study in depth how command exit statuses work in Bash and how scripts rely on them. It is designed for people who already have a foundational knowledge of Linux and Bash and covers how exit codes are produced, interpreted and used to drive the flow of a script.
As everywhere in the academy, theory comes first and practice happens strictly in natural language: you will not type scripts by hand. You will describe to the machine, in plain words, how a script must react to success and failure, then read the code it proposes, check where it tests an exit status or enables pipefail, and verify by running it that every error path really behaves as you asked.
Key Learning Objectives:
- Understand Exit Status Basics:
- Learn what an exit status is and why it is the way a command signals success or failure.
-
Explore the common exit statuses (0 for success, non-zero for errors) and how they are interpreted.
-
Interpreting Exit Codes:
- Learn to read and understand the different exit codes returned by system commands.
-
Gain insight into the standard conventions and the specific meanings associated with certain exit codes.
-
Practical Examples of Capture:
- Understand how the exit code of a command is captured using
$?and how options likeset -o pipefailchange it, so you can ask the machine for the right technique and recognise it in its output. -
Analyse scripts generated on your instructions and judge whether they handle and respond to each exit status correctly.
-
Conditional Execution Based on Exit Status:
- Learn how conditional statements (e.g. if-else) let a script act on the success or failure of commands, and describe that logic precisely in natural language.
-
Review advanced error-handling techniques proposed by the machine and spot weak or missing checks.
-
Real-world Scripting Scenarios:
-
Work through real-life scenarios where exit codes are crucial, such as automated backups, system maintenance tasks and deployment processes, instructing the machine and verifying that failures are detected and reported.
-
Best Practices:
- Learn the best practices that make scripts reliable and maintainable through correct use of exit statuses, and require them explicitly in your instructions.
- Ask for comments documenting the expected exit statuses and check that they match what the code actually does.
Module Content:
-
Introduction to Exit Codes:
A guide to the basics of exit codes in Unix-like systems, covering both the theory and its practical implications. -
Capturing and Using Exit Codes:
Step-by-step walkthroughs of the techniques Bash offers to capture exit codes, so that you can recognise them in the scripts the machine writes and explain when each one is appropriate. -
Practical Examples:
-
Example Scripts (requested in natural language, then reviewed):
- Simple Command Execution: Scenarios that check the success of basic commands.
- Error Handling: Error-handling strategies that capture non-zero exit statuses and respond accordingly.
- Complex Workflows: Scripts where multiple commands are chained, ensuring each step runs only when the previous outcome allows it.
-
Natural-language Exercises:
Hands-on exercises in which you instruct the machine to build and debug Bash scripts based on exit statuses, deliberately provoke failures, and verify that its scripts react exactly as you specified.
Target Audience:
This module is intended for system administrators, DevOps professionals, developers and others working with Bash, with a particular focus on the practical use of command exit statuses.
By the end of this module, participants will understand how exit statuses make Bash scripts reliable, and will be able to guide the machine to produce such scripts and verify them.
The students can push their exercises to the Academy DevOps & SRE Git project. For this module, create a folder with your username as its name in the following subfolder: https://github.com/Garanti-Del-Talento/gdt_academy
