[ norboten ]
linux · lab linux-04

Write the Report

· about 40 minutes · runs on ubuntu-26.04, alpine · unrated

An unrated lab. It runs on your machine with no account and no network, and everything about it — the faults, the checks, the hints and the reference solution — is in the repository. An attempt on it is recorded on your profile and never moves a rating: only rated labs do. Rated and unrated labs.

The briefing

Operations wants a quick way to see who is hammering the web server. Write /usr/local/bin/top-talkers, a Bash script, to this specification. A sample log is in /srv/logs/access.log.

top-talkers [-n N] FILE
  • FILE is an access log in common log format: the first field of each line is the client address.
  • Print the N busiest client addresses (default 5), one per line, as <count> <address> — a single space between them.
  • Order by count, highest first. When counts are equal, order by address as plain text (byte order: 10.0.0.10 before 10.0.0.9).
  • Ignore empty lines and lines that start with #.
  • -n takes a positive whole number. Anything else: print top-talkers: invalid count to stderr and exit with status 2.
  • If FILE is missing or unreadable: print top-talkers: cannot read FILE (with the actual name) to stderr and exit with status 1.
  • An empty log, or one with only comments: print nothing, exit 0.
  • Bash and the standard tools only — no Python, Perl, Ruby, PHP or Node.

Your script is graded by running it against logs you have not seen, including every edge case above.

What is graded

The machine is checked, rebooted, and checked again. A check passes only if it passes both times.

CheckObjective
01_top_five_by_defaultProcess text with pipelines: filter, count, sort
02_count_option_and_tiesProcess text with pipelines: filter, count, sort
03_skips_comments_and_blanksProcess text with pipelines: filter, count, sort
04_rejects_bad_countsHandle options and arguments in a Bash script
05_missing_file_and_empty_inputReport errors correctly: messages on stderr, meaningful exit status
06_pure_bashHandle options and arguments in a Bash script

Start it

  1. 2Labs
  2. linux-04select this one
  3. udownload it
  4. sstart the machine

Reading

Where the lab's hints send you, level by level, as you ask for them (h, then l opens a journal section in the TUI).

Theory for this lab

3 questions on the same topic, in the lab's Theory tab. They never affect the lab's grade. Here they are, to answer in place:

Question 1

Why does `uniq -c` need its input sorted?

Question 2

What does this print?

printf '10.0.0.9\n10.0.0.10\n' | LC_ALL=C sort | head -1
Question 3

Which sort orders "count address" lines by count descending, then address ascending?