Signal9Signal9 — home
online
back to labs

The Script That Ate the Logs

MediumShell Scripting20-30 min
The brief

/usr/local/sbin/prune-logs runs every night to clear old ledger logs, and it is fragile in every classic way: it parses ls, leaves every expansion unquoted, matches names with an unanchored regex and validates nothing. It has been deleting the wrong files and choking on others. Rewrite it to its contract, prune the real ledger with it, and recover the release token from a recent ledger log.

The contract, prune-logs DIR DAYS:

  • Deletes the regular files directly in DIR whose name ends in .log and that are older than DAYS days (find -mtime +DAYS is fine; grading never tests the exact boundary).
  • Deletes nothing else: not newer .log files, not names that merely contain log (ledger-catalog.txt, x.log.gz), not directories, not files in subdirectories.
  • Works with any file name, including spaces and a leading dash.
  • Exits 0 on success. With the wrong number of arguments, a DIR that is empty or not an existing directory, or a DAYS that is not a whole number, it exits non-zero and deletes nothing.

Grading runs your script on fresh directories built at grading time. Then:

1. Prune the real ledger: prune-logs /var/log/ledger 30. Everything that should stay must still be there, unchanged.

2. Save the release token (the flag) from the recent ledger log to /root/case/flag.txt.

See /root/case/brief.txt.

What you'll do
  • Spot why parsing ls, unquoted expansions and unanchored matches break on real file names
  • Rewrite a script to an explicit contract, including argument validation and exit codes
  • Run the repaired tool on real data without destroying anything that must stay

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