← Lessons/Loops & Conditionals

Loops & conditionals

Almost every useful script boils down to one sentence: "do this for each of those, but only when some condition holds." Restart each service in a list. Back up every file in a directory. Retry until something comes up healthy. The two tools that express this are loops (do it for each) and conditionals (only when). You met if in the last lesson; here you pair it with repetition.

bashloopsconditionals

Interview prep

Control flow shows you can actually script, not just run commands.

When would you reach for a while loop instead of a for loop?

for is for a known list (files, a range). while runs until a condition changes — reading a stream line by line, retrying until something succeeds, polling until a service is up.