Control Flow Structures (if-else, case, for, while, until)

Control flow एक script को सोचने लायक बनाता है: if-else और case paths के बीच choose करते हैं, for, while, और until काम को repeat करते हैं, तो आपका backup script decide कर सकता है क्या करना है और एक बार straight through चलने की बजाय हर file पर loop कर सकता है।

12 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Scripts जो Decide और Repeat करती हैं

एक script जो बस अपनी lines top से bottom, सिर्फ़ एक बार चलाती है, useful है पर limited है। Real scripts को decide करना पड़ता है (अगर backup folder exist नहीं करता, इसे create करो) और repeat करना पड़ता है (list की हर file copy करो)। यही control flow है, और आप इन ideas को Python और C में पहले मिल चुके हैं; यहाँ आप shell का version सीखते हैं।

दो families हैं: branching (if-else और case, एक path choose करना) और looping (for, while, until, काम repeat करना)। यह lesson सारी पाँच cover करता है, shell के particular syntax और इसके distinctive closing keywords के साथ।

Theory

Branching: if-else और case

if एक block सिर्फ़ तब चलाता है जब एक test succeed करता है, optional elif (else-if) और else branches के साथ, और यह fi (if reversed) से close होता है:

if [ test ]; then ... elif [ test ]; then ... else ... fi

case एक value को कई patterns के against compare करता है, एक single variable match करते समय elifs की एक लंबी chain से cleaner। हर pattern ;; से ख़त्म होता है, और पूरा block esac (case reversed) से close होता है। General true/false conditions के लिए if इस्तेमाल कीजिए, और जब आप एक value को कई specific possibilities के against check कर रहे हों तब case।

Practical

if-elif-else और case (verified)

n=7
if [ $n -lt 5 ]; then
    echo "small"
elif [ $n -lt 10 ]; then
    echo "medium"          # n=7 -> prints medium
else
    echo "large"
fi

fruit=apple
case $fruit in
    apple)  echo "It is an apple" ;;   # this matches
    banana) echo "It is a banana" ;;
    *)      echo "Unknown" ;;          # * = anything else
esac

Theory

Looping: for, while, until

for list के हर item के लिए एक बार repeat करता है, 'हर file पर यह करो' के लिए perfect:

for f in *.txt; do ... done

while तब तक repeat करता है जब तक एक condition true रहती है: while [ test ]; do ... done। until इसकी mirror image है: यह तब तक repeat करता है जब तक एक condition true नहीं बन जाती (यानी, condition अभी भी false है तब तक)। तीनों loops done से close होते हैं।

तो while [ $i -le 3 ] तब तक चलता रहता है जब तक i, 3 या इससे कम है, और until [ $i -gt 3 ] तब तक चलता रहता है जब तक i, 3 से exceed नहीं करता, दोनों, 1 से counting करते हुए, 1, 2, 3 print करते हैं। जिसका condition सबसे naturally पढ़ा जाए वह choose कीजिए।

Practical

for, while, और until (verified)

# for: iterate over a list
for f in *.txt; do
    echo "file: $f"
done

# while: repeat WHILE the condition is true
i=1
while [ $i -le 3 ]; do
    echo "while $i"        # prints while 1, while 2, while 3
    i=$((i+1))
done

# until: repeat UNTIL the condition becomes true
i=1
until [ $i -gt 3 ]; do
    echo "until $i"        # also prints 1, 2, 3
    i=$((i+1))
done

Formula

Closing Keywords का ध्यान रखिए

Shell हर block को एक specific word से close करता है, और इन्हें mix करना एक common error है:

if, fi से close होता है, case, esac से close होता है (दोनों keyword backwards spelled हैं), और हर loop, for, while, until, done से close होता है।

यह भी याद रखिए हर case pattern के बाद ;;, और test [ ... ] को brackets के अंदर spaces चाहिए। यही छोटी syntax details हैं जो exactly shell beginners को trip up करती हैं, तो इन्हें deliberately type कीजिए।

Quiz

यह loop क्या print करता है? i=1; while [ $i -le 3 ]; do echo $i; i=$((i+1)); done

  1. 1 2 3 4, 4 तक count करते हुए
  2. 1 2 3, हर number अपनी line पर
  3. कुछ नहीं, क्योंकि condition शुरुआत में false है
  4. एक infinite loop
Show the answer

1 2 3, हर number अपनी line पर

Loop तब तक चलता है जब तक i, 3 या इससे कम है। i=1 से शुरू करते हुए: यह 1 print करता है (फिर i, 2 बनता है), 2 print करता है (i, 3 बनता है), 3 print करता है (i, 4 बनता है), और अब 4, <= 3 नहीं है, तो यह रुक जाता है, 1, 2, 3 देते हुए, हर एक अपनी line पर। Option A एक off by one है: जब i, 4 तक पहुँचता है condition [ 4 -le 3 ] false है, तो 4 कभी print नहीं होता। Option C ग़लत है: शुरुआत में i=1, और [ 1 -le 3 ] true है, तो loop actually चलता है। Option D ग़लत है: क्योंकि i हर pass में बढ़ता है (i=$((i+1))), condition आख़िर में fail होती है और loop ख़त्म हो जाता है, यह infinite नहीं है। Counter carefully trace कीजिए: -le 3, 3 के बाद रुकता है।

Think first

while या until: आप कैसे Choose करें?

while और until mirror images हैं। हर एक कब ज़्यादा naturally पढ़ता है? फिर tap कीजिए।

Show the answer

जो भी CONDITION को एक plain-English intention की तरह पढ़ने दे उसे choose कीजिए, क्योंकि ये logical opposites हैं। while तब तक repeat करता है जब तक इसका condition TRUE है, तो यह तब fit होता है जब आप सोचते हैं 'कुछ hold होने तक चलते रहो': जब तक अभी भी process करने के लिए files हैं, जब तक user ने quit type नहीं किया, जब तक counter range के अंदर है। until तब तक repeat करता है जब तक इसका condition FALSE है, true बनते ही रुकते हुए, तो यह तब fit होता है जब आप सोचते हैं 'कुछ happen होने तक चलते रहो': जब तक server respond नहीं करता, जब तक download complete नहीं होता, जब तक counter limit exceed नहीं करता। दोनों को हमेशा test flip करके एक-दूसरे में rewrite किया जा सकता है, while [ $i -le 3 ] और until [ $i -gt 3 ] exactly same values 1, 2, 3 पर loop करते हैं, तो choice READABILITY के बारे में है, capability के बारे में नहीं। वह phrasing पिक कीजिए जो match करे आप naturally stopping point कैसे describe करते हैं, और आपकी script पढ़ने में आसान और less error-prone बन जाती है। अगर आप खुद को एक negated, awkward condition वाला while लिखते पाएँ, एक until इसे ज़्यादा clearly express कर सकता है, और vice versa। Same loop, clearer intent।

Summary

Key takeaways

  • Control flow एक script को decide (branching) और repeat (looping) करने देता है, exactly Python और C की तरह पर shell syntax के साथ।
  • if-then-elif-else-fi tests से एक path choose करता है; case एक value को patterns के against match करता है और esac से close होता है।
  • for एक list पर iterate करता है (for f in *.txt); हर file पर कुछ करने के लिए ideal।
  • while तब तक repeat करता है जब तक इसका condition true है; until तब तक repeat करता है जब तक इसका condition true नहीं बनता (mirror image)।
  • Closing keywords matter करते हैं: if, fi से close होता है, case, esac से, और सारे loops, done से।
  • while [ $i -le 3 ], 1 से counting करते हुए, 1, 2, 3 print करता है, और i, 4 तक पहुँचने पर रुकता है।
  • Memory hook: if/case decide करते हैं, for/while/until repeat करते हैं; close करने के लिए fi, esac, done।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Shell Scripting in Linux

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Control Flow Structures (if-else, case, for, while, until) · Linux Operating System (LOS) (Minor-04) · Gri-Learn