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

case Statement का उपयोग

case आपको एक value को कई possible patterns के against compare करने देता है और जो भी match करे उसके लिए अलग code चलाने देता है, if/elif की एक लंबी chain लिखे बिना।
Syntax
bash
case $variable in
  pattern1)
    commands
    ;;
  pattern2|pattern3)
    commands
    ;;
  *)
    default_commands
    ;;
esac

Basic case Syntax

case एक single value को patterns की एक list के against order में compare करता है और पहले matching pattern के नीचे के commands चलाता है, हर branch को ;; से खत्म करते हुए। पूरे statement को esac से बंद किया जाता है।

उदाहरण: Basic case Syntax

bash
#!/bin/bash
fruit="banana"
case $fruit in
    apple)
        echo "It's an apple"
        ;;
    banana)
        echo "It's a banana"
        ;;
    *)
        echo "Unknown fruit"
        ;;
esac

| के साथ Multiple Patterns Match करना

एक single branch | (pipe) से separate करके कई alternative patterns match कर सकती है, जो patterns के बीच एक or की तरह काम करता है। यह same commands को कई separate branches में duplicate करने से बचाता है।

उदाहरण: Matching Multiple Patterns with |

bash
#!/bin/bash
answer="y"
case $answer in
    y|Y|yes|YES)
        echo "Confirmed"
        ;;
    n|N|no|NO)
        echo "Declined"
        ;;
    *)
        echo "Unrecognized response"
        ;;
esac

Glob Patterns in case

case patterns filename matching जैसे ही glob wildcards उपयोग करते हैं: * किसी भी string से match करता है, ? एक single character से match करता है, और [abc] listed characters में से किसी एक से match करता है। यह case को file extensions जैसी चीज़ें classify करने के लिए convenient बनाता है।

उदाहरण: Glob Patterns in case

bash
#!/bin/bash
filename="archive.tar.gz"
case $filename in
    *.txt)
        echo "Text file"
        ;;
    *.tar.gz|*.tgz)
        echo "Compressed tarball"
        ;;
    *)
        echo "Unknown type"
        ;;
esac

Fallthrough with ;;&

Normally case पहली matching branch चलाने के बाद रुक जाता है, लेकिन Bash-specific ;;& terminator execution को एक match के बाद भी बाद के patterns test करते रहने देता है, statement से exit होने के बजाय।

यह तब useful है जब एक से ज़्यादा branch legitimately same value पर apply हो सकती हो।

उदाहरण: Fallthrough with ;;&

bash
#!/bin/bash
value="15"
case $value in
    [0-9]*)
        echo "Starts with a digit"
        ;;&
    1*)
        echo "Starts with a 1"
        ;;
esac
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. हर pattern के commands के end में double semicolon ;; भूल जाना; इसके बिना Bash अगले ;; की तलाश करता रहता है और branches को अप्रत्याशित रूप से merge कर देता है।
  2. यह सोचना कि case patterns regular expressions हैं; वे असल में glob-style wildcard patterns (*, ?, [...]) हैं, पूरा regex syntax नहीं।
  3. Statement खत्म करने के लिए closing esac (case backwards spelled) भूल जाना।
चैप्टर सारांश
  • Syntax: case value in pattern1) commands ;; pattern2) commands ;; esac।
  • Patterns glob wildcards (*, ?, [abc]) support करते हैं और एक branch में कई alternatives match करने के लिए | से combine किए जा सकते हैं, जैसे y|Y)।
  • *) final pattern के रूप में एक catch-all default branch की तरह काम करता है, पहले न पकड़ी गई किसी भी चीज़ से match करते हुए।
  • ;;& (Bash extension) execution को एक match के बाद भी subsequent patterns test करते रहने देता है, पहले match पर रुकने के बजाय।

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.