Hands-on Lab

#Lab 01: Automation Script Project

Build a complete automation script with proper structure.

#🎯 Objectives

  • Create a production-quality script
  • Implement proper error handling
  • Add logging and documentation

#📋 Task

Create a system-report.sh script that generates a system report.


#Implementation

Create system-report.sh:

bash
1#!/bin/bash
2set -euo pipefail
3
4# =============================================================================
5# Script: system-report.sh
6# Description: Generate comprehensive system report
7# Usage: ./system-report.sh [--output FILE]
8# =============================================================================
9
10OUTPUT_FILE=""
11
12usage() {
13    echo "Usage: $0 [--output FILE]"
14    exit 1
15}
16
17log() {
18    echo "[$(date +'%H:%M:%S')] $*"
19}
20
21parse_args() {
22    while [[ $# -gt 0 ]]; do
23        case $1 in
24            --output) OUTPUT_FILE="$2"; shift 2 ;;
25            *) usage ;;
26        esac
27    done
28}
29
30generate_report() {
31    echo "======================================"
32    echo "System Report - $(date)"
33    echo "======================================"
34    echo ""
35    echo "=== System Info ==="
36    uname -a
37    echo ""
38    echo "=== CPU Usage ==="
39    uptime
40    echo ""
41    echo "=== Memory Usage ==="
42    free -h
43    echo ""
44    echo "=== Disk Usage ==="
45    df -h
46    echo ""
47    echo "=== Top Processes ==="
48    ps aux --sort=-%cpu | head -6
49    echo ""
50    echo "======================================"
51}
52
53main() {
54    parse_args "$@"
55    log "Generating system report..."
56    
57    if [ -n "$OUTPUT_FILE" ]; then
58        generate_report > "$OUTPUT_FILE"
59        log "Report saved to: $OUTPUT_FILE"
60    else
61        generate_report
62    fi
63}
64
65main "$@"

#✅ Success Criteria

  • Script runs without errors
  • Output is well-formatted
  • Can save to file
  • Handles errors gracefully