← Lessons/Conditionals & Tests

Conditionals & tests

A script that always does the same thing is just a recording. The moment it has to react (does this file exist? is this variable empty? did that command succeed?) it needs to make a decision. That's what conditionals are for, and they're the difference between a snippet you run by hand and one you'd trust to run unattended in CI.

This sandbox has a config.yaml but no secret.key: two steps, one test each.

bashconditionalstests

Interview prep

File/empty tests are the backbone of safe scripts.

Why guard with [ -f config ] before reading a file in a script?

So the script fails gracefully instead of erroring (or worse, acting on missing data). Defensive checks — file exists, variable non-empty, previous command succeeded — are what separates a throwaway snippet from a script you'd run in CI.