Module Title: dgt_bash03 – Functions
Module Description:
This module, “dgt_bash03 – Functions,” covers Bash functions, the feature that lets scripts grow without becoming unmanageable. It is intended for beginners and intermediate users who want to understand how Bash functions really work: how they are defined, how they receive arguments, what they return and how they share variables with the rest of the script.
Here you will not write functions by hand. After studying the theory, you describe to the machine in natural language the function you need (“take a file and a size limit, default to 1000 bytes, and tell me whether the file exceeds it”), and it writes the Bash. Your task is to read that code critically: check the syntax, the quoting, the handling of $? and of local variables, run it against edge cases, and send it back with precise corrections when it is wrong.
Module Objectives:
-
Understand Function Basics: Learn what functions are in Bash scripting and why they are essential for keeping scripts manageable.
-
Recognize Well-Structured Functions: Learn what a clear, precise function definition looks like, so you can ask for reusable, maintainable functions and recognize when the machine’s version falls short.
-
Parameter Handling: Learn how parameters are passed to functions and used inside scripts, so you can specify exactly which inputs a function should accept.
-
Return Values: Explore how functions return values and exit statuses, and how these drive data flow and decision-making in a script.
-
Scope and Variable Management: Understand the scope of variables in Bash functions and how to spot unintended side effects in the code the machine generates.
Key Topics Covered:
- Introduction to Functions in Bash
- Definition and importance
-
Benefits of using functions in scripting
-
Creating and Defining Functions
- Basic syntax for defining functions
-
Naming conventions and best practices
-
Function Parameters and Arguments
- Passing single and multiple arguments
-
Accessing positional parameters within a function
-
Returning Values from Functions
- Using
returnstatements effectively -
Working with special variables like
$? -
Reviewing Bash Functions Written by the Machine
Throughout the module, you will request a range of functions in natural language and examine what comes back. The drafts below are typical first answers: not all of them are correct, and working out exactly what is wrong, and how to ask for the fix, is part of the exercise.
-
Example 1: Greeting Function
Ask the machine for a function that takes a name as an argument and prints a personalized greeting.bash
greet {
echo "Hello, $1!"
} -
Example 2: File Size Checker
Ask for a function that checks the size of a file and reports whether it exceeds a specified limit.bash
check_file_size {
local file=$1
local max_size=${2:-1000} # Default maximum size is 1000 bytes
if [[ $stat -c%s "$file" -gt $max_size ]]; then
echo "File $file exceeds the limit of $max_size bytes."
else
echo "File $file is within the size limit."
fi
} -
Example 3: Sum Function
Ask for a function that calculates the sum of two numbers.bash
sum {
local num1=$1
local num2=$2
echo $num1 + num2
} -
Advanced Topics in Bash Functions
- Recursive functions
- Local and global variables
Module Outcomes:
By the end of this module, you will understand how functions work in Bash scripts well enough to describe modular, reusable functions to the machine with precision and to judge whether the code it writes is correct. Through the examples, you will have trained your eye to catch syntax errors, wrong expansions and scope problems before they reach production.
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
