Signal9Signal9 — home
online
back to labs

Debug It: Trace a Broken Script with set -x

MediumShell Scripting15-25 min
The brief

/root/scripts/sum.sh is supposed to total the integers in /root/case/nums.txt, but it prints the wrong thing. Don't guess — watch it run.

Trace the script and read what each line actually expands to. The bug is visible in the trace: one line isn't doing the arithmetic you'd assume it is.

Fix the script so it prints SUM=<the correct total>. Grading runs your fixed script and checks the total, then runs it again with a different nums.txt in place (so compute the total from the file, don't print a number), and confirms the script does real shell arithmetic (any form: $(( )), (( )), let, declare -i, expr) rather than hardcoding the answer. See /root/case/brief.txt.

What you'll do
  • Trace the script with bash -x / set -x to locate the bug
  • Recognise string concatenation where arithmetic was intended
  • Fix it with arithmetic expansion $(( ... ))
  • Produce the correct SUM from the data file

Hands-on lab: you work on a real Linux box in your browser, graded on the machine's actual state.