Interview Prep/Shell Scripting/Scenarios: text processing and scheduling decisions

Management wants a weekly email with disk usage of all servers as an HTML table. How do you build it?

BeginnerScenarioScenarios: text processing and scheduling decisions
Answer

Situation read → Collect data, render HTML with a here-doc, send with a mail tool.

What I'd do →

{
  cat <<'HTML'
<html><body><h3>Weekly disk usage</h3><table border="1" cellpadding="4">
<tr><th>Host</th><th>Mount</th><th>Used</th><th>Size</th><th>Use%</th></tr>
HTML
  for h in "${hosts[@]}"; do
    ssh -n -o ConnectTimeout=5 "$h" 'df -hP -x tmpfs -x devtmpfs' | awk -v h="$h" 'NR > 1 {
      style = ($5+0 >= 85) ? " style=\"background:#fdd\"" : ""
      printf "<tr%s><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n", style, h, $6, $3, $2, $5}'
  done
  echo '</table></body></html>'
} > /tmp/disk-report.html
mail -a 'Content-Type: text/html' -s "Disk usage $(date +%F)" [email protected] < /tmp/disk-report.html
  1. The mail -a header syntax is for GNU Mailutils; s-nail and msmtp differ, so test on your distro.
  2. Highlight rows above 85% so readers see problems at a glance.

Why → A here-doc plus awk is enough for simple HTML; no templating engine needed.

Interview tip → Mention that a Grafana dashboard with alerts is better than an email nobody reads, but reports still have their place.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: text processing and scheduling decisions