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

Nested और Compound Conditionals

Real-world scripts को अक्सर कई related conditions एक साथ जांचने की ज़रूरत होती है, या तो if-blocks को एक दूसरे के अंदर nest करके या conditions को एक single test में combine करके।
Syntax
bash
if [[ condition1 && ( condition2 || condition3 ) ]]; then
  commands
fi

एक [[ ]] में Conditions Combine करना

Multiple conditions को && और || उपयोग करके सीधे एक single [[ ]] test के अंदर combine किया जा सकता है, जब checks independent हों तो nested if blocks की ज़रूरत से बचते हुए। यह related logic को visually साथ grouped रखता है।

उदाहरण: Combining Conditions in One [[ ]]

bash
#!/bin/bash
age=25
has_ticket=true
if [[ $age -ge 18 && $has_ticket = true ]]; then
    echo "Entry granted"
else
    echo "Entry denied"
fi

Parentheses के साथ Grouping

[[ ]] के अंदर, parentheses ( ) sub-expressions को group करके evaluation order explicitly control कर सकते हैं, math expressions जैसा। बिना grouping के, && और || को mix करना एक ऐसे order में evaluate हो सकता है जो आपने intend नहीं किया था।

उदाहरण: Grouping with Parentheses

bash
#!/bin/bash
role="editor"
is_owner=false
if [[ ($role = "admin" || $role = "editor") && $is_owner = false ]]; then
    echo "Limited edit access granted"
fi

Nesting if कब ज़्यादा Clear है

कभी-कभी एक बाद की condition को evaluate करना सिर्फ तब sense बनाता है जब एक पहली वाली पहले से pass हो गई हो, खासकर जब दूसरी check अन्यथा error out होगी या meaningless होगी (जैसे एक file की contents जांचना सिर्फ file के exist करने की confirm करने के बाद)।

इन cases में, nested if blocks सब कुछ एक compound expression में cram करने से ज़्यादा clear हैं।

उदाहरण: When Nesting if Is Clearer

bash
#!/bin/bash
file="data.txt"
echo "sample" > "$file"
if [ -f "$file" ]; then
    if [ -s "$file" ]; then
        echo "$file exists and is non-empty"
    else
        echo "$file exists but is empty"
    fi
else
    echo "$file does not exist"
fi

Guard Clauses से Deep Nesting को Flatten करना

कई if blocks nest करने के बजाय, पहले failure cases जांचना और early exit/continue करना (एक 'guard clause') अक्सर flatter, ज़्यादा readable code produce करता है। हर guard main logic चलने से पहले एक specific unwanted condition handle करता है।

उदाहरण: Flattening Deep Nesting with Guard Clauses

bash
#!/bin/bash
check_input() {
    local value="$1"
    if [ -z "$value" ]; then
        echo "Rejected: empty value"
        return 1
    fi
    if [ "$value" -lt 0 ] 2>/dev/null; then
        echo "Rejected: negative value"
        return 1
    fi
    echo "Accepted: $value"
}
check_input ""
check_input "-5"
check_input "42"
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. if blocks को कई levels गहराई तक over-nest करना जब [[ ]] के अंदर &&/|| के साथ एक single compound condition कहीं ज़्यादा readable होगी।
  2. बिना parentheses के && और || mix करते समय operator precedence भूल जाना; [[ ]] double brackets के अंदर ( ) के साथ grouping respect करता है, और इसे skip करना silently बदल सकता है कि असल में कौन सी conditions साथ जांची जा रही हैं।
  3. -a/-o के साथ compound conditions के लिए [ ] उपयोग करना; ये deprecated हैं और complex expressions के साथ unreliably behave करते हैं -- &&/|| के साथ [[ ]] modern, safer choice है।
चैप्टर सारांश
  • Conditions को एक [[ ]] में && (and), || (or) उपयोग करके combine किया जा सकता है, और explicit precedence के लिए ( ) से grouped किया जा सकता है।
  • Nested if blocks तब appropriate हैं जब बाद की checks सिर्फ तब sense बनाती हैं/safe होती हैं जब पहली वाली पहले से pass हो गई हों।
  • [[ ]] sub-expressions को group करने के लिए parentheses support करता है; [ ] इसे reliably support नहीं करता, जो compound logic के लिए [[ ]] prefer करने का एक और कारण है।
  • Deeply nested conditionals आमतौर पर इशारा करते हैं कि logic को compound &&/|| expressions या early return/continue guards उपयोग करके flatten किया जा सकता है।

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.