FDD-book/examples/part-01/ch03-a-gentle-introduction-to-unix/shell-scripting/07-functions.sh
Edson Brandi 6e6dac94fe Add book companion files under examples/
Organize companion code and materials by book part:

  part-01/      Foundations
  part-02/      Kernel & driver fundamentals
  part-03/      Character & block devices
  part-04/      Networking & storage
  part-05/      Buses & advanced topics
  part-06/      Debugging & performance
  part-07/      Real-world drivers
  appendices/   Supplementary material
  README.md     Overview and build instructions
2026-04-25 16:51:00 +00:00

40 lines
870 B
Bash

#!/bin/sh
# functions.sh - Demonstrates using functions and command-line arguments in a shell script.
#
# Usage:
# ./functions.sh NUM1 NUM2
# Example:
# ./functions.sh 5 7
# This will output: "[INFO] 5 + 7 = 12"
# A simple function to print informational messages
say() {
# "$1" represents the first argument passed to the function
echo "[INFO] $1"
}
# A function to sum two integers
sum() {
# "$1" and "$2" are the first and second arguments
local a="$1"
local b="$2"
# Perform arithmetic expansion to add them
echo $((a + b))
}
# --- Main script execution starts here ---
# Make sure the user provided two arguments
if [ $# -ne 2 ]; then
echo "Usage: $0 NUM1 NUM2"
exit 1
fi
say "Beginning work"
# Call the sum() function with the provided arguments
result="$(sum "$1" "$2")"
# Print the result in a nice format
say "$1 + $2 = $result"