-
Create
functions.shwith:- A function
greetthat takes a name as argument and printsHello, <name>! - A function
addthat takes two numbers and prints their sum - Call both functions from the script
- A function
-
Create
disk_check.shwith:- A function
check_diskthat checks disk usage of/usingdf -h - A function
check_memorythat checks free memory usingfree -h - A main section that calls both and prints the results
- A function
- Create
strict_demo.shwithset -euo pipefailat the top - Try using an undefined variable — what happens with
set -u? - Try a command that fails — what happens with
set -e? - Try a piped command where one part fails — what happens with
set -o pipefail?
Document: What does each flag do?
-
set -e→ Exit the script immediately if any command fails. -
set -u→ Exit the script if an undefined (unset) variable is used. -
set -o pipefail→ Pipeline fails if any command fails.
-
Create
local_demo.shwith:- A function that uses
localkeyword for variables - Show that
localvariables don't leak outside the function - Compare with a function that uses regular variables
- A function that uses
Create system_info.sh that uses functions for everything:
-
A function to print hostname and OS info
-
A function to print uptime
-
A function to print disk usage (top 5 by size)
-
A function to print memory usage
-
A function to print top 5 CPU-consuming processes
-
A
mainfunction that calls all of the above with section headers -
Use
set -euo pipefailat the top
Functions & Modularity – Learned to create reusable, organized code blocks.This makes scripts cleaner, easier to read, and simpler to maintain.
System Monitoring Scripts – Explored fetching system info like memory,disk usage,and CPU processes.Useful for building quick automation for system health checks.
Error Handling & Safety – Using set -euo pipefail to catch undefined variables,failing commands,and pipeline errors early,making scripts more reliable.
Variable Scope – Understood the difference between local and global variables. Local variables stay inside functions, while global variables affect the wider script.
Practical Automation – Using a main function to orchestrate tasks helps make scripts modular,maintainable,and automation-friendly.
Function Naming Pitfall – Faced an issue where naming a function the same as a system command (uptime) caused an infinite loop. Learned to avoid using system command names for functions.





