case Statement का उपयोग
In this page:
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
#!/bin/bash
fruit="banana"
case $fruit in
apple)
echo "It's an apple"
;;
banana)
echo "It's a banana"
;;
*)
echo "Unknown fruit"
;;
esac
Login to try C/C++/Java/PHP code in the editor
| के साथ Multiple Patterns Match करना
एक single branch | (pipe) से separate करके कई alternative patterns match कर सकती है, जो patterns के बीच एक or की तरह काम करता है। यह same commands को कई separate branches में duplicate करने से बचाता है।
उदाहरण: Matching Multiple Patterns with |
#!/bin/bash
answer="y"
case $answer in
y|Y|yes|YES)
echo "Confirmed"
;;
n|N|no|NO)
echo "Declined"
;;
*)
echo "Unrecognized response"
;;
esac
Login to try C/C++/Java/PHP code in the editor
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
#!/bin/bash
filename="archive.tar.gz"
case $filename in
*.txt)
echo "Text file"
;;
*.tar.gz|*.tgz)
echo "Compressed tarball"
;;
*)
echo "Unknown type"
;;
esac
Login to try C/C++/Java/PHP code in the editor
Fallthrough with ;;&
Normally case पहली matching branch चलाने के बाद रुक जाता है, लेकिन Bash-specific ;;& terminator execution को एक match के बाद भी बाद के patterns test करते रहने देता है, statement से exit होने के बजाय।
यह तब useful है जब एक से ज़्यादा branch legitimately same value पर apply हो सकती हो।
उदाहरण: Fallthrough with ;;&
#!/bin/bash
value="15"
case $value in
[0-9]*)
echo "Starts with a digit"
;;&
1*)
echo "Starts with a 1"
;;
esac
Login to try C/C++/Java/PHP code in the editor
- हर pattern के commands के end में double semicolon
;;भूल जाना; इसके बिना Bash अगले;;की तलाश करता रहता है और branches को अप्रत्याशित रूप से merge कर देता है। - यह सोचना कि
casepatterns regular expressions हैं; वे असल में glob-style wildcard patterns (*,?,[...]) हैं, पूरा regex syntax नहीं। - 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 पर रुकने के बजाय।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: