The Script That Ate the Logs
MediumShell Scripting20-30 min/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
DIRwhose name ends in.logand that are older thanDAYSdays (find -mtime +DAYSis fine; grading never tests the exact boundary). - Deletes nothing else: not newer
.logfiles, not names that merely containlog(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
DIRthat is empty or not an existing directory, or aDAYSthat 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.
- 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.
