Linux, terminal and Git

Stage 7 of 8 · 8 lessons · ~3 weeks

Shell scripts and automation

Turn repeated commands into small, safe Bash scripts, check them with ShellCheck, schedule them with cron and keep your own configuration in a dotfiles repository.

Start: Anatomy of a script

Lessons· one lesson for each topic, in this order

  1. 1Anatomy of a script
  2. 2Failing safely: exit codes and set -euo pipefail
  3. 3Variables, arguments and input
  4. 4Conditionals and loops
  5. 5Functions and readable scripts
  6. 6Checking scripts with ShellCheck
  7. 7Scheduling with cron
  8. 8Dotfiles in a Git repository

Do this

  • Write a script with a shebang, `set -euo pipefail`, argument checks and a usage message.
  • Make it executable and put it in a folder on your PATH.
  • Run ShellCheck and fix every warning, reading the explanation for each.
  • Add a loop over files and a function with `local` variables.
  • Schedule it with cron using full paths and a log file, and confirm it ran.
  • Create a dotfiles repository with your shell and Git config and an install script.
  • Run the install script twice to check it is safe to repeat.

Start with· follow one of these

Practise

Keep handy

  • Paste or run a script to catch quoting and error-handling bugs before they bite

  • Check what each part of an unfamiliar command does before you put it in a script

Also worth your time

Before moving on: A public dotfiles repository with an idempotent `install.sh` that passes ShellCheck with no warnings, plus one useful script of your own (a backup, a cleanup or a report) running from cron and writing to a log.

Tauseef's advice
Automate something you actually do every week, however small. A 20-line script that saves you five minutes every day teaches more than any tutorial. Run ShellCheck on everything, and switch to Python the moment Bash starts to feel like a puzzle.

Mark this stage as not done?