तार्किक Operators
In this page:
command1 && command2 # run command2 if command1 succeeds
command1 || command2 # run command2 if command1 fails
! command
Sequential Success के लिए &&
&& अपने right वाले command को सिर्फ तब चलाता है जब इसके left वाला command status 0 के साथ exit हुआ हो। यह उन steps को chain करने का एक compact तरीका है जो सिर्फ तब आगे बढ़ने चाहिए जब पिछला काम कर गया हो, एक पूरा if block लिखे बिना।
उदाहरण: && for Sequential Success
#!/bin/bash
mkdir -p output_dir && echo "Directory ready, continuing"
cd output_dir && echo "Now inside output_dir: $(pwd)"
Login to try C/C++/Java/PHP code in the editor
Failure पर Fallback के लिए ||
|| अपने right वाले command को सिर्फ तब चलाता है जब इसके left वाला command एक non-zero status के साथ exit हुआ हो। यह आमतौर पर एक fallback action या error message देने के लिए उपयोग किया जाता है जब कुछ fail हो।
उदाहरण: || for Fallback on Failure
#!/bin/bash
grep -q "xyz" <<< "abc def" || echo "Pattern 'xyz' was not found, using fallback"
Login to try C/C++/Java/PHP code in the editor
एक Condition को Negate करने के लिए !
! इसके ठीक बाद आने वाले command या test का exit status invert करता है, success को failure में और failure को success में बदलते हुए। [ ] या [[ ]] के अंदर, इसे negate की जा रही condition के ठीक पहले रखा जाता है।
उदाहरण: ! to Negate a Condition
#!/bin/bash
if [ ! -f nonexistent_file.txt ]; then
echo "Confirmed: nonexistent_file.txt does not exist"
fi
if ! grep -q "zzz" <<< "hello world"; then
echo "Confirmed: pattern not found"
fi
Login to try C/C++/Java/PHP code in the editor
&& और || को साथ Combine करना
&& और || को एक line पर एक compact 'if success do X, else do Y' pattern के लिए chain किया जा सकता है, strictly left से right evaluated होते हुए। ज़्यादा complex combinations के साथ careful रहें -- एक simple two-way branch से आगे किसी भी चीज़ के लिए, एक proper if/else clearer और less error-prone है।
&&/|| chain में middle command खुद उन कारणों से fail हो सकता है जो condition से unrelated हैं, तो || fallback अप्रत्याशित रूप से चल सकता है -- जब logic complex हो जाए तो if/else prefer करें।उदाहरण: Combining && and || Together
#!/bin/bash
value=10
[ "$value" -gt 5 ] && echo "Value is greater than 5" || echo "Value is 5 or less"
Login to try C/C++/Java/PHP code in the editor
- यह सोचना कि
&&/||true/false booleans पर operate करते हैं; वे असल में एक command के exit status (0 = success) के आधार पर chain करते हैं, इसलिएcommand1 && command2का असल मतलब है 'command2 सिर्फ तब चलाएं अगर command1 0 exited हुआ'। [ ]बनाम[[ ]]के अंदर negate करते समय!को गलत जगह रखना;!को expression के ठीक पहले जाना चाहिए ([ ! -f file ]), पूरेifके पहले नहीं।- बिना parentheses/grouping के कई
&&/||को साथ chain करना और result से surprised होना, क्योंकि&&और||equal precedence के साथ left से right evaluated होते हैं, कुछ दूसरी languages के विपरीत।
cmd1 && cmd2cmd2को सिर्फ तब चलाता है जबcmd1status 0 (succeeded) के साथ exit हुआ हो।cmd1 || cmd2cmd2को सिर्फ तब चलाता है जबcmd1एक non-zero status (failed) के साथ exit हुआ हो।!इसके बाद आने वाले command या test का exit status negate करता है -- success failure बन जाता है और vice versa।[[ ]]के अंदर,&&और||sub-conditions को सीधे combine करते हैं;[[ ]]/(( ))के बाहर, वे पूरे commands combine करते हैं।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: