Linux, terminal and Git

Stage 7 · Lesson 4 of 8

Conditionals and loops

Shell scripts and automation

Scripts make decisions with if and repeat work with . if [[ -f "$file" ]]; then ... fi a condition. Common tests are -f (a file exists), -d (a folder exists), -z (a is empty), == (two strings match) and -gt (one number is greater than another). case picks between several neatly.

for f in *.csv; do ... done loops over files. while read -r line; do ... done < file reads a file one line at a time. Keep the logic simple; when a script needs complex data, move to .

Common traplooping over $(ls), which breaks on spaces in file names, instead of using a glob directly.

A question to check yourself, an AI practice session and 2 cheat sheet entries come when you add the roadmap to your account. Add it

Resources for this stage

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

Mark this stage as not done?