Bitpath

Boss lesson · Unit 01

Automate a chore

From commands you retype to a script, then a named task that fails loudly

13 exercisesabout 3 min

What you practise

  • A repeated sequence becomes a script, then a named task in a Makefile or package.json
  • Arguments, exit codes, failing fast

Try a question from this lesson

Pick one

Before each push you run lint, then the tests. You save both in a script; false stands in for a lint step that fails. What does the last line print?

shell
cat > check.sh <<'EOF'
echo lint; false
echo test
EOF
bash check.sh; echo "exit $?"

The ideas

Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.

Concept

The exit status is the interface

Everything that runs your script (make, a git hook, CI, &&) asks one question: did it exit 0?

So a task script has two duties: stop at the first failure, and pass the failure on. One line at the top does both.

shell
#!/bin/bash
set -euo pipefail
echo lint; false
echo test

Concept

Give the chore a name

A Makefile names your chores. Each rule is a target, then the targets it needs first, then indented commands. The indent must be a tab, written here as \t. @ stops make from echoing the command.

make check runs the rule, and a failing command stops everything.

shell
printf 'check: lint test\n' > Makefile
printf 'lint:\n\t@echo lint\n' >> Makefile
printf 'test:\n\t@echo test\n' >> Makefile
make check

10 more exercises in the app

  • 3× type the answer
  • 2× fill the blanks
  • 3× multiple choice
  • 1× true or false
  • 1× order the lines

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy