Bitpath

Boss lesson · Unit 04

A backup script that fails loudly

set -euo pipefail, quoting disasters, find, tar and cron

13 exercisesabout 3 min

What you practise

  • set -euo pipefail and what each flag stops, unquoted-variable disasters, find ... -exec and find | xargs, tar and date in a filename, running it from cron

Try a question from this lesson

Pick one

A nightly backup script. One night /data/site is missing, so cd fails. What does the script do next?

shell
cd /data/site
tar -czf /backups/site.tar.gz .
echo "backup done"

The ideas

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

Concept

set -e: stop at the first failure

set -e changes the default: the script exits the moment a command fails.

A failure you handle yourself does not count. That covers the command after if, and the left side of && or ||.

shell
#!/bin/bash
set -e
cd site          # fails: script stops
tar -czf site.tar.gz .
echo "backup done"

Concept

The safety line

Three switches, written as one line at the top of every script:

-e exit when a command fails.

-u treat an unset variable as an error.

-o pipefail a pipeline fails if any stage fails, not only the last.

Together they turn silent damage into a loud stop at the line that went wrong.

shell
#!/bin/bash
set -euo pipefail

10 more exercises in the app

  • 3× type the answer
  • 4× multiple choice
  • 1× true or false
  • 1× fill the blanks
  • 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