Bash by Example: System Info Report
Generating a comprehensive HTML system status report combining multiple system metrics into a formatted web dashboard, implementing automated report generation for system monitoring, creating visual status displays with HTML formatting, integrating multiple data sources into unified reports, and building lightweight monitoring dashboards.
Code
#!/bin/bash
OUTPUT="system_report.html"
TITLE="System Status Report"
# Gather Info
HOSTNAME=$(hostname)
UPTIME=$(uptime -p)
KERNEL=$(uname -r)
DISK=$(df -h / | awk 'NR==2 {print $5 " used (" $4 " free)"}')
MEM=$(free -h | grep Mem | awk '{print $3 " / " $2}')
# Generate HTML
cat <<EOF > "$OUTPUT"
<!DOCTYPE html>
<html>
<head>
<title>$TITLE</title>
<style>
body { font-family: sans-serif; max-width: 800px; margin: 2rem auto; }
h1 { color: #2563eb; border-bottom: 2px solid #eee; padding-bottom: 0.5rem; }
.card { background: #f9fafb; padding: 1.5rem; border-radius: 8px; margin-bottom: 1rem; }
.label { font-weight: bold; color: #4b5563; }
.value { color: #111827; }
</style>
</head>
<body>
<h1>$TITLE</h1>
<p>Generated on: $(date)</p>
<div class="card">
<p><span class="label">Hostname:</span> <span class="value">$HOSTNAME</span></p>
<p><span class="label">Kernel:</span> <span class="value">$KERNEL</span></p>
<p><span class="label">Uptime:</span> <span class="value">$UPTIME</span></p>
</div>
<div class="card">
<p><span class="label">Disk Usage (/):</span> <span class="value">$DISK</span></p>
<p><span class="label">Memory Usage:</span> <span class="value">$MEM</span></p>
</div>
</body>
</html>
EOF
echo "Report generated: $OUTPUT"Explanation
Bash scripts aren't limited to plain text output. Because text is text, Bash can easily generate structured formats like HTML, JSON, XML, or Markdown. This is extremely powerful for creating dashboards, email reports, or static status pages.
This script collects various system metrics using standard commands (uptime, df, free) and then uses a Here Document to inject them into an HTML template. The result is a professional-looking report that can be viewed in any web browser.
The technique of "templating" with Here Documents (cat <<EOF) is simple yet effective. Variables inside the block are expanded, allowing you to mix static HTML structure with dynamic data. For more complex reporting, you might output JSON and let a frontend framework handle the display, but for simple daily reports, this method is unbeatable.
Code Breakdown
awk 'NR==2 ...'. Commands like df print a header line first. NR==2 tells awk to only process the second row (the actual data), ensuring we don't grab the column titles.< allows us to write multi-line HTML code directly in the script without needing a separate template file. This makes the script self-contained and easy to distribute. 
