Stage 7 · Lesson 4 of 8
Conditionals and loops
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
- GuideFreeGreg's Wiki: BashGuide
Greg's Wiki (Wooledge) · Beginner
A careful, accurate Bash guide that teaches the habits that avoid most scripting bugs
- CourseFreeThe Missing Semester of Your CS Education
MIT · Beginner
The shell tools and scripting lecture, with exercises that build useful small scripts
Practise
Keep handy
Also worth your time
- roadmap.sh: Shell / BashGo deeper
The scripting side of the shell map: arrays, traps, parameter expansion and more