$_ CLI In Depth
Learning progress 0%
Build · Performance, Debugging & Power Tools

Build: A System Health Report Script

Write a health report script with sections, strict mode, and an output file option.

~40 min Advanced 3 checked steps

Build it on a real Linux machine

The guided lab boots Linux in your browser. Each step runs a check script on that machine, so it passes only when your files and output are right.

What you will build

Step 1: Write the report with three sections

Write health.sh. It prints a heading and the output of one command for memory, disk, and load:

BASH10 lines
cat > health.sh <<'EOF'
#!/bin/bash
echo "== Memory =="
free -m
echo "== Disk =="
df -h /workspace
echo "== Load =="
cat /proc/loadavg
EOF
bash health.sh > report.txt

Open report.txt. It should have the headings Memory, Disk, and Load.

Step 2: Turn on strict mode

Strict mode stops a script early when something fails. Add set -euo pipefail on the line after the shebang:

BASH3 lines
sed -i '1a set -euo pipefail' health.sh
bash health.sh > report.txt
echo "exit status: $?"

The report should still show all three headings, and the script should finish with status 0.

Step 3: Write to a file with -o

Let the user choose the output file. Rewrite health.sh so that -o FILE sends the report to FILE, and with no option it prints to standard output:

BASH12 lines
#!/bin/bash
set -euo pipefail
out=/dev/stdout
if [ "${1:-}" = "-o" ]; then out="$2"; fi
{
  echo "== Memory =="
  free -m
  echo "== Disk =="
  df -h /workspace
  echo "== Load =="
  cat /proc/loadavg
} > "$out"

Run bash health.sh -o summary.txt and open summary.txt.

Files

  • health.sh: the finished script.
  • walkthrough.sh: every step of the reference solution, in order.

How it is checked

Each step in the guided lab has a check script that runs on the lab machine after you finish. It passes only when the files and output it expects are there, so the lab cannot be completed by reading alone.

Reference files

Read these after you have tried the lab. They are the solutions the checks were verified against.

health.sh

BASH12 lines
#!/bin/bash
set -euo pipefail
out=/dev/stdout
if [ "${1:-}" = "-o" ]; then out="$2"; fi
{
  echo "== Memory =="
  free -m
  echo "== Disk =="
  df -h /workspace
  echo "== Load =="
  cat /proc/loadavg
} > "$out"

walkthrough.sh

BASH37 lines
#!/usr/bin/env bash
# Reference solution for every step of the guided lab, in order.

# Step 1: Write the report with three sections
cat > /workspace/health.sh <<&#039;EOF&#039;
#!/bin/bash
echo "== Memory =="
free -m
echo "== Disk =="
df -h /workspace
echo "== Load =="
cat /proc/loadavg
EOF
bash /workspace/health.sh > /workspace/report.txt

# Step 2: Turn on strict mode
cd /workspace
sed -i &#039;1a set -euo pipefail&#039; health.sh
bash health.sh > /workspace/report.txt

# Step 3: Write to a file with -o
cd /workspace
cat > health.sh <<&#039;EOF&#039;
#!/bin/bash
set -euo pipefail
out=/dev/stdout
if [ "${1:-}" = "-o" ]; then out="$2"; fi
{
  echo "== Memory =="
  free -m
  echo "== Disk =="
  df -h /workspace
  echo "== Load =="
  cat /proc/loadavg
} > "$out"
EOF
bash health.sh -o summary.txt

Lessons this build uses