Exercise: Read the Logbook
Take over a lighthouse station by reading its notice, its long logbook and its supplies with cat, less and file, look up a new command in its manual page, and let a check script tell you which answers are right.
Apprentice · Unix · Linux · Updated
You're the new keeper at Gull Rock Light Station. The last keeper left you a notice, a logbook of almost a year of days, nearly a thousand lines long, and a folder of supplies, one of which isn't what its name suggests. Your job is to answer eight questions that only a careful reader can: when the whale came and went, how the logbook ends, which file isn't text, and two things you'll find only in the manual. A check script compares your answers with the station's files and tells you which ones are right.
Before you start
You need a terminal: the Terminal app on a Mac, or Ubuntu on Linux or in WSL on Windows. The check runs with bats, a small program that runs checks written for bash. If you did Exercise: Find Your Way, you already have it. If bats --version doesn't print a version number yet, install it:
sudo apt install bats # Ubuntu, and Ubuntu in WSL
brew install bats-core # a Mac, with Homebrew
sudo asks for your password, the one you use to sign in; nothing shows on the screen while you type it.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-bash-read-the-logbook, also linked from the Exercise box beside the lesson. Click Code, then Download ZIP, and unzip it into your terminal-practice folder. Or clone it with Git.
The folder holds:
station/: the notice, the logbook and the supplies folder. Read them; don't change them.answers.txt: the eight questions, each with a numbered line for your answer. This is the file you write.check.bats: the check. You don't need to change it.
What to do
Open a terminal and cd into the exercise folder, the one with answers.txt in it. Then cd station and start with the notice. Each question in answers.txt looks like this, with the example already answered:
# Example: what is the name of the station? (notice.txt says so in its first line.)
# 0) Gull Rock
# 1. Read notice.txt in the station folder. Who wrote it? (Write the name only.)
1)
- Read.
catthe short files. Openlogbook.txtwithless, and search it rather than paging through every day. - Look it up. Two questions are about commands: one you haven't met yet,
head, and one about which command is built into the shell.man,--helpandtypehave the answers. - Answer. Open
answers.txtin any text editor, write each answer after its), and save. Write dates the way the logbook writes them, such asMarch 3.
Check your work
From the exercise folder (not from inside station):
bats check.bats
At first almost every check fails. That's expected: each answer you get right turns its check into a ✓. A failing check names its question and gives a hint, for example Question 3: "June 21" isn't the last whale yet. After a search in less, n jumps to the next match. Keep pressing it until there are no more.
Hints
- The logbook is long. In
less,/searches,njumps to the next match andGto the end (Reading Files: cat and less). The match moves to the top of the screen, so press the up arrow to see the date line above it. - Not sure what a file is? Ask
filebefore you open it. Ifcatever fills your screen with strange symbols, typeresetand press Enter. - Counting lines is a job for a command from Getting Help. Its
--helpshows which option counts only lines. - A command you've never seen:
manand its name, then search the page with/the way you search inless(Getting Help). - Typing the same search again? The up arrow and Ctrl+R bring it back (Your Command History).
When you're done
Every check passes:
check.bats
✓ the station folder is still here
✓ 1) who wrote the notice
✓ 2) the first time the logbook mentions a whale
✓ 3) the last time the logbook mentions a whale
✓ 4) the date of the last entry
✓ 5) how many lines the logbook has
✓ 6) the file in supplies that isn't text
✓ 7) the head option that sets how many lines it prints
✓ 8) the command that's built into the shell
9 tests, 0 failures
Now look back at question 7. You learned what an option of head does without anyone teaching you. Try the option on the logbook with a few different numbers: does it do what the manual said?
Submit your exercise
The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.
On GitHub, click +, then New repository. Name it (for example
exercise-bash-read-the-logbook), choose Public, and click Create repository. Leave out the README: your folder already has one.In a terminal in your exercise folder (Ubuntu in WSL on Windows), send your work to your new repo. If you downloaded the ZIP:
git init -b main git add . git commit -m "My solution" git remote add origin https://github.com/your-name/exercise-bash-read-the-logbook.git git push -u origin mainIf you cloned the starter instead, point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-bash-read-the-logbook.git git add . git commit -m "My solution" git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-bash-read-the-logbook, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.
Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.
So what
Reading a long file for the one line that matters, and working out a command from its own manual, is what developers and administrators do every day with logs, settings and tools they've never used before.