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:
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.txtOpen 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:
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:
#!/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.