if/elif/else का उपयोग
In this page:
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
#!/bin/bash
age=20
if [ "$age" -ge 18 ]; then
echo "Adult"
else
echo "Minor"
fi
Login to try C/C++/Java/PHP code in the editor
elif Branches जोड़ना
elif ('else if' का short form) आपको additional conditions को sequence में जांचने देता है जब पहली if condition false थी। Bash उन्हें top से bottom evaluate करता है और सिर्फ उस पहली branch को execute करता है जिसकी condition succeed होती है।
उदाहरण: Adding elif Branches
#!/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
Login to try C/C++/Java/PHP code in the editor
if किसी भी Command के साथ, सिर्फ Tests नहीं
क्योंकि if सिर्फ एक command का exit status जांचता है, आप इसके बाद कोई भी command रख सकते हैं, सिर्फ [ ] या [[ ]] tests नहीं। यह आमतौर पर इस आधार पर branch करने के लिए उपयोग किया जाता है कि कोई दूसरा program succeed हुआ या नहीं, जैसे grep का एक match ढूंढना।
उदाहरण: if With Any Command, Not Just Tests
#!/bin/bash
if grep -q "root" /etc/passwd; then
echo "Found a line mentioning root"
else
echo "No match found"
fi
Login to try C/C++/Java/PHP code in the editor
Multiple Checks के लिए Nested if
एक if block इसके अंदर एक और पूरा if/fi block रख सकता है, जो तब useful है जब एक दूसरी condition जांचना सिर्फ तब sense बनाता है जब पहली वाली पहले से pass हो गई हो।
Readability के लिए nesting shallow रखें -- conditions को && से combine करना अक्सर ज़्यादा clear होता है।
उदाहरण: Nested if for Multiple Checks
#!/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
Login to try C/C++/Java/PHP code in the editor
- Condition के बाद mandatory
; thenभूल जाना (याthenको बिना semicolon के same line पर रखना), जो एक syntax error produce करता है। - यह मान लेना कि
ifदूसरी languages की तरह एक boolean expression evaluate करता है; यह असल में एक command चलाता है और इसका exit status जांचता है -- 0 का मतलब हैifbranch ली जाती है, कुछ भी और का मतलब है नहीं। ifblock बंद करने के लिएfiछोड़ देना, या इसे गलत spell करना (यहifbackwards spelled है,endifनहीं)।
ifएक command चलाता है (अक्सर[ ]या[[ ]]) और branch decide करने के लिए exit status लेता है, सीधे एक true/false value नहीं।- Block
fiसे खत्म होना चाहिए, और हर condition line को; thenया अपनी own line परthenचाहिए। elifadditional conditions chain करता है;elseतब के लिए एक fallback provide करता है जब कोई match न हो।- सिर्फ पहली matching branch (top से bottom) चलती है; एक बार कोई match हो जाए तो बाद के
elif/elseblocks skip हो जाते हैं।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: