Pcornel
Automation · Scripting

Loops

Cheat sheet · https://pcornel.dk/topic.php?topic=5216

Back to the Topic

Answer

Loops repeat logic across items or until a condition changes. Safe automation requires bounded iteration, per-item failure visibility, stable collection semantics and an explicit completion condition.

Remember

  • Loop complete ≠ every iteration successful; failures can be skipped, swallowed or overwritten by later success.
  • Retry loop ≠ resilience automatically; retrying a non-idempotent side effect can duplicate or compound change.
  • Parallel loop ≠ faster safely; shared state, rate limits and target dependencies can create races or blast radius.
  • Until condition not met ≠ keep waiting forever; timeouts and escalation boundaries are required.

First checks

  • shellcheck script.sh — flags unsafe loops (SC2013 for-over-cat)
  • while IFS= read -r line; do …; done < file — safe line loop
  • foreach ($i in $list) { … } — PowerShell

Verify in this order

list → loop → per-item action + error handling

Where it usually breaks

Loop termination proves only that iteration control reached an end condition; it does not prove all items succeeded or that repeated actions were side-effect safe.

Script/input → command/module binding → local/remoting session → request accepted → command result → target object read-back → effective runtime state → workload.

Loops scripting boundary map

BoundaryHealthy evidenceSuspicious evidence
Input / runtimeEffective inputs, runtime, working context and dependency versions are explicitHidden defaults, stale environment, wrong interpreter/path or unvalidated input
Logic / executionBranch/function/module decisions and external actions expose per-operation resultsSwallowed failures, shell reinterpretation, blind retries or last-result-wins behavior
Result / proofExit/exception/log evidence is correlated with independent target read-backProcess success or printed output treated as target success
Recovery / troubleshootingFirst failed boundary is preserved, changed minimally and retestedRandom edits, catch-all suppression or evidence destroyed before diagnosis

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=5216

Confirmation

Confirm action

Continue?