← Back to Bash Course | Chapter 4: Loops | Lesson 8 of 14

until Loop का उपयोग

एक until loop while का mirror image है: यह तब तक repeat करता रहता है जब तक इसकी condition false रहे, condition आखिरकार true होते ही रुकते हुए।
Syntax
bash
until [ condition ]; do
  commands
done

Basic until Loop

until condition; do ... done body को बार-बार चलाता है जब तक condition false (non-zero exit status) evaluate हो, पहली बार true होने पर रुकते हुए। यह 'इसे तब तक करो जब तक कोई goal reach न हो जाए' के लिए naturally पढ़ता है।

उदाहरण: Basic until Loop

bash
#!/bin/bash
count=1
until [ "$count" -gt 5 ]; do
    echo "Count is $count"
    count=$((count + 1))
done
echo "Stopped once count exceeded 5"

until vs while ! condition

until condition और while ! condition एक simple command-based condition के लिए वही looping behavior produce करते हैं, क्योंकि until essentially test के sense को invert किया हुआ while है।

दोनों में choose करना सिर्फ इस बात का मामला है कि situation के लिए कौन सा ज़्यादा clearly पढ़ता है।

उदाहरण: until vs while ! condition

bash
#!/bin/bash
n=0
until [ "$n" -ge 3 ]; do
    echo "until: n=$n"
    n=$((n + 1))
done
n=0
while ! [ "$n" -ge 3 ]; do
    echo "while!: n=$n"
    n=$((n + 1))
done

Polling-Style Retry with until

until का एक आम real उपयोग है एक operation को retry करना जब तक वह succeed न हो जाए (या maximum attempts reach न हो जाएं), जो 'success तक keep retrying' के रूप में पढ़ता है।

अगर condition कभी actually true न हो तो हमेशा के लिए loop करने से बचने के लिए इसे एक counter के साथ combine करें।

उदाहरण: Polling-Style Retry with until

bash
#!/bin/bash
attempt=0
succeeded=false
until [ "$succeeded" = true ] || [ "$attempt" -ge 3 ]; do
    attempt=$((attempt + 1))
    echo "Trying operation, attempt $attempt"
    if [ "$attempt" -eq 2 ]; then
        succeeded=true
    fi
done
if [ "$succeeded" = true ]; then
    echo "Succeeded on attempt $attempt"
else
    echo "Gave up after $attempt attempts"
fi

Condition That Starts True Skips the Body

चूंकि until अपनी condition को बहुत पहले iteration से पहले check करता है, अगर वह condition पहले से true हो, तो loop body कभी एक बार भी नहीं चलती। Bash में कोई built-in do-while/do-until construct नहीं है जो कम से कम एक execution की गारंटी दे -- वह pattern ज़रूरत पड़े तो manually simulate किया जाना चाहिए।

उदाहरण: Condition That Starts True Skips the Body

bash
#!/bin/bash
already_done=true
until [ "$already_done" = true ]; do
    echo "This line never prints"
done
echo "Loop body was skipped because the condition started true"
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. until को while ! के साथ confuse करना; वे simple conditions के लिए logically equivalent हैं, लेकिन until ज़्यादा naturally पढ़ता है जब आप 'X होने तक keep going' express कर रहे हों, 'not X के दौरान keep going' नहीं।
  2. यह भूल जाना कि, while की तरह, condition हर iteration से पहले फिर से check होती है -- सिर्फ शुरुआत में एक बार नहीं।
  3. एक until condition लिखना जो शुरुआत में ही true हो, जिससे loop body बिल्कुल कभी execute न हो, जो यह उम्मीद करने वाले लोगों को surprise करता है कि कम से कम एक run होगा (Bash में कोई built-in do-until construct नहीं है)।
चैप्टर सारांश
  • Syntax: until condition; do ... done -- body को तब तक चलाता है जब तक condition false हो, true होने पर रुकते हुए।
  • until condition वैसा ही behave करता है जैसे while ! condition (command exit-status conditions के लिए)।
  • अगर condition पहले check से पहले ही true हो, तो body बिल्कुल नहीं चलती -- कोई built-in do-until नहीं है।
  • until अक्सर polling-style logic के लिए चुना जाता है: 'यह succeed होने तक keep trying'।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.