← Back to Bash Course | Chapter 3: Control Flow | Lesson 2 of 14

if/elif/else का उपयोग

if/elif/else आपके script को यह चुनने देता है कि कौन सा block of commands चलाना है इस आधार पर कि एक condition true है या false।
Syntax
bash
if [ condition ]; then
  commands
elif [ condition ]; then
  commands
else
  commands
fi

Basic if/else

if keyword इसके बाद आने वाले command को चलाता है और इसका exit status जांचता है: 0 (success) if branch लेता है, कुछ भी और else की तरफ fall through करता है। Block को fi से बंद किया जाना चाहिए।

उदाहरण: Basic if/else

bash
#!/bin/bash
age=20
if [ "$age" -ge 18 ]; then
    echo "Adult"
else
    echo "Minor"
fi

elif Branches जोड़ना

elif ('else if' का short form) आपको additional conditions को sequence में जांचने देता है जब पहली if condition false थी। Bash उन्हें top से bottom evaluate करता है और सिर्फ उस पहली branch को execute करता है जिसकी condition succeed होती है।

उदाहरण: Adding elif Branches

bash
#!/bin/bash
score=75
if [ "$score" -ge 90 ]; then
    echo "Grade: A"
elif [ "$score" -ge 75 ]; then
    echo "Grade: B"
elif [ "$score" -ge 60 ]; then
    echo "Grade: C"
else
    echo "Grade: F"
fi

if किसी भी Command के साथ, सिर्फ Tests नहीं

क्योंकि if सिर्फ एक command का exit status जांचता है, आप इसके बाद कोई भी command रख सकते हैं, सिर्फ [ ] या [[ ]] tests नहीं। यह आमतौर पर इस आधार पर branch करने के लिए उपयोग किया जाता है कि कोई दूसरा program succeed हुआ या नहीं, जैसे grep का एक match ढूंढना।

उदाहरण: if With Any Command, Not Just Tests

bash
#!/bin/bash
if grep -q "root" /etc/passwd; then
    echo "Found a line mentioning root"
else
    echo "No match found"
fi

Multiple Checks के लिए Nested if

एक if block इसके अंदर एक और पूरा if/fi block रख सकता है, जो तब useful है जब एक दूसरी condition जांचना सिर्फ तब sense बनाता है जब पहली वाली पहले से pass हो गई हो।

Readability के लिए nesting shallow रखें -- conditions को && से combine करना अक्सर ज़्यादा clear होता है।

उदाहरण: Nested if for Multiple Checks

bash
#!/bin/bash
logged_in=true
is_admin=false
if [ "$logged_in" = true ]; then
    if [ "$is_admin" = true ]; then
        echo "Welcome, admin"
    else
        echo "Welcome, user"
    fi
fi
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. Condition के बाद mandatory ; then भूल जाना (या then को बिना semicolon के same line पर रखना), जो एक syntax error produce करता है।
  2. यह मान लेना कि if दूसरी languages की तरह एक boolean expression evaluate करता है; यह असल में एक command चलाता है और इसका exit status जांचता है -- 0 का मतलब है if branch ली जाती है, कुछ भी और का मतलब है नहीं।
  3. if block बंद करने के लिए fi छोड़ देना, या इसे गलत spell करना (यह if backwards spelled है, endif नहीं)।
चैप्टर सारांश
  • if एक command चलाता है (अक्सर [ ] या [[ ]]) और branch decide करने के लिए exit status लेता है, सीधे एक true/false value नहीं।
  • Block fi से खत्म होना चाहिए, और हर condition line को ; then या अपनी own line पर then चाहिए।
  • elif additional conditions chain करता है; else तब के लिए एक fallback provide करता है जब कोई match न हो।
  • सिर्फ पहली matching branch (top से bottom) चलती है; एक बार कोई match हो जाए तो बाद के elif/else blocks skip हो जाते हैं।

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.