Get AI summaries of any video or article — Sign up free
a BASH script PUSH-UP counter (for #gains ) thumbnail

a BASH script PUSH-UP counter (for #gains )

NetworkChuck·
5 min read

Based on NetworkChuck's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

Use while loops to repeat work while a condition remains true, and update the loop variable inside the loop to ensure termination.

Briefing

A bash push-up counter built with loops turns basic scripting into something immediately useful: it repeatedly prompts, counts, and stops based on simple conditions. Starting from a “while” loop that increments a counter until a limit is reached, the script demonstrates how bash can automate repetitive tasks—then quickly pivots to a more interactive version that asks for user input (press Enter to continue) and lets the user choose how many push-ups to do. The practical takeaway is that loops aren’t just programming exercises; they’re a way to control repetition, pacing, and termination in real workflows.

The lesson then broadens from “while” to “until,” framing “until” as the upside-down counterpart: “while condition is true, keep looping” versus “until condition becomes true, keep looping.” A coffee-or-tea example makes the difference concrete. The loop keeps asking for input until the user types “coffee,” at which point the condition flips and the script ends. That same pattern—looping until a goal state is reached—reappears later in a network-check scenario.

Next comes the “for” loop, introduced as the tool for iterating over a known list of values. A simple cups-of-coffee example shows how a loop variable takes on each item in a sequence (1 through 10), and the script runs once per item. The transcript also highlights a shorthand range syntax using braces (e.g., {1..10}), which makes loops easier to read and maintain.

The most “real-world” uses arrive with two for-loop examples. One script iterates over a list of domains, pings each one quietly, and uses an if/else check to print whether each site is up or down. Another script reads city names from a text file and uses curl to fetch current weather for each city, echoing results directly in the terminal. Together, these examples show how loops combine with command-line tools to batch-check connectivity and automate data retrieval.

Because loops can run forever, the transcript spends time on safety controls. A “while true” example is intentionally dangerous—then stopped with Ctrl+C—before introducing “break” as the clean escape hatch. A connectivity monitor loop uses “break” to exit as soon as a ping succeeds, avoiding endless repetition. Finally, “continue” is demonstrated in an “elevator” style loop: when the loop variable hits 13, the script skips that iteration and moves on, producing a sequence that omits a specific value.

By the end, the core message is that bash’s while, until, and for loops—plus break and continue—form a small set of building blocks that unlock automation. Whether counting reps, validating user input, checking servers, or pulling weather for many cities, the same control-flow patterns keep repetition predictable and stop conditions under control.

Cornell Notes

Loops in bash let scripts repeat work based on conditions or lists. A while loop can increment a counter until it reaches a limit, and adding read prompts turns repetition into an interactive routine (like a push-up counter). An until loop runs in the opposite direction—asking for input until a target value (e.g., “coffee”) is entered. A for loop iterates over a sequence or a list (including ranges like {1..10}), enabling batch tasks such as pinging multiple domains and fetching weather for cities read from a file. Because loops can run forever, break exits immediately when a success condition is met, and continue skips a single iteration (e.g., omitting floor 13).

How does a while loop decide when to stop in the push-up counter example?

The script sets a counter variable (X) and uses a condition like “while [[ $X -le 100 ]]”. The loop body runs between “do” and “done”, and each iteration increments X (X++). When X becomes greater than 100, the condition fails, so the loop ends. In the later push-up version, the limit is reduced (e.g., to 10) and the loop pauses for user input using read (press Enter to continue).

What’s the practical difference between while and until, and how is it shown with the coffee/tea prompt?

A while loop continues while a condition is true; an until loop continues until a condition becomes true. In the coffee/tea example, the loop runs “until order equals coffee”. If the user types “tea” or anything else, the condition isn’t met, so it keeps asking. Only when the user enters “coffee” does the condition become true and the loop stops.

Why are for loops especially useful when the script has a known set of values?

A for loop iterates over a predefined list, assigning the loop variable one item per iteration. The cups example defines a list (1..10) and echoes “Hey, you’ve had X cups of coffee today” each time X changes. The transcript also shows brace range syntax (e.g., {1..10}) as a cleaner way to express sequences.

How does the domain connectivity script determine whether a site is up or down?

It loops over a list of domains and pings each one quietly (using options to suppress output and limit attempts/time). An if statement checks the ping result: if a response is received, it prints “domain is up”; otherwise it prints “domain is down”. The loop variable (X) changes each iteration, so the same ping logic applies to every domain in the list.

What do break and continue do, and when would each be used?

break exits the entire loop immediately. In the “while true” connectivity check, the loop runs forever until a ping succeeds; then break stops the script. continue skips only the current iteration and moves to the next one. In the elevator example, when X equals 13, continue prevents printing that floor, but the loop continues for subsequent floors.

Review Questions

  1. In the push-up counter, what exact condition controls loop termination, and what statement updates the counter each iteration?
  2. How would you rewrite a “while” loop into an “until” loop for the same stopping behavior? Provide the condition transformation.
  3. In the connectivity-check loop, where should break be placed to stop immediately after the first successful ping?

Key Points

  1. 1

    Use while loops to repeat work while a condition remains true, and update the loop variable inside the loop to ensure termination.

  2. 2

    Add read prompts to make loops interactive, letting users control pacing (e.g., press Enter to continue).

  3. 3

    Treat until loops as the inverse of while: keep looping until a target condition becomes true.

  4. 4

    Use for loops to iterate over known lists or ranges, enabling batch operations like pinging many domains or processing many cities from a file.

  5. 5

    Prefer break over Ctrl+C for safety when a loop might otherwise run forever; break exits as soon as the success condition is met.

  6. 6

    Use continue to skip a single iteration without stopping the entire loop, such as omitting a specific value (floor 13).

Highlights

A while loop with a counter can drive an automated push-up count, stopping when the counter exceeds a set threshold.
An until loop keeps asking for input until the user enters the target value—coffee ends the loop, tea doesn’t.
A for loop can batch-check connectivity by pinging each domain and printing up/down status based on the ping result.
A for loop can also automate weather retrieval by reading city names from a text file and calling curl for each city.
break and continue provide loop control: break ends the loop on success, while continue skips just one iteration.

Topics

  • Bash Loops
  • While Loop
  • Until Loop
  • For Loop
  • Ping and Curl Automation

Mentioned

  • SSH
  • API