Signal9Signal9 — home
online
back to labs

Error Handling: trap and Exit Codes

MediumShell Scripting10-20 min
The brief

Robust scripts clean up after themselves and signal failure with an exit code. trap runs a handler when the script exits; a non-zero exit code tells callers something went wrong.

Write /root/scripts/risky.sh that:

1. creates a scratch file /root/out/work.tmp,

2. registers a trap that removes /root/out/work.tmp on EXIT,

3. then fails on purpose, exiting with code 3.

Because the trap fires on the way out, work.tmp must NOT remain after the script runs — and the exit status must be 3. Grading runs your script, then checks both the exit code and that the scratch file was cleaned up.

What you'll do
  • Register a trap that removes the scratch file on EXIT
  • Exit with status 3 while leaving no /root/out/work.tmp behind

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