Logical Operators (&&, ||, !)

Shell success से commands chain कर सकता है: double ampersand अगली command सिर्फ़ तब चलाता है जब पिछली succeed हुई हो, और double pipe अगली सिर्फ़ तब चलाता है जब पिछली fail हुई हो, आपको एक single line में and-then और or-else logic build करने देते हुए।

9 min read · 7 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

And-Then, Or-Else

अक्सर आप चाहते हैं एक command सिर्फ़ तब चले जब दूसरी काम कर गई हो। एक directory बनाओ and then इसमें move करो, पर सिर्फ़ अगर बनाना succeed हुआ हो। एक file खोलने की कोशिश करो, or else एक warning print करो अगर यह fail हो जाए।

Shell इसे दो logical operators से express करता है: && (and) और || (or)। ये commands को इसके based पर chain करते हैं कि हर एक succeed या fail हुआ। इन्हें समझने के लिए, आपको पहले एक idea चाहिए: exit status।

Theory

Exit Status: Success Zero है

हर command, जब यह ख़त्म होती है, अपने exit status नाम का एक hidden number return करती है। Convention है: 0 का मतलब success है, और कोई भी non-zero value का मतलब failure है। (आप last वाला echo $? से देख सकते हैं।)

यह उससे opposite है जो आप guess करें, zero वह 'good' result है, पर यह sense बनाता है: succeed होने का एक तरीका है, पर fail होने के कई अलग-अलग तरीके हैं, तो non-zero values अलग-अलग errors signal कर सकते हैं। Logical operators अगली command चलानी है या नहीं decide करने के लिए इस status इस्तेमाल करते हैं।

Practical

Action में && और || (verified)

# && runs the next command ONLY if the first succeeded (exit 0)
$ mkdir demo && cd demo        # cd only happens if mkdir worked
$ true && echo "ran"
ran
$ false && echo "skipped"      # first failed, so this is skipped
$ 

# || runs the next command ONLY if the first FAILED (non-zero)
$ false || echo "fallback"     # first failed, so this runs
fallback
$ true || echo "skipped"       # first succeeded, so this is skipped
$

Formula

&& है 'And Then', || है 'Or Else'

इन्हें loudly पढ़िए और ये खुद को explain कर देते हैं। `A && B` का मतलब है 'A चलाओ, and then B सिर्फ़ तब चलाओ अगर A succeed हुआ'। `A || B` का मतलब है 'A चलाओ, or else B सिर्फ़ तब चलाओ अगर A fail हुआ'।

तो mkdir demo && cd demo सिर्फ़ तब safely directory में enter करता है अगर यह create हुई हो, और ping -c1 host || echo "host down" सिर्फ़ तब एक warning print करता है जब ping fail हो। && और || से chain करना आपको बिना एक पूरे if statement के safe, self-checking one-liners लिखने देता है।

Quiz

यह क्या print करता है? false && echo "A" ; true || echo "B"

  1. A और B दोनों
  2. न A, न B
  3. सिर्फ़ A
  4. सिर्फ़ B
Show the answer

न A, न B

हर part लीजिए। 'false && echo A' में, पहली command false FAIL होती है, और && दूसरी सिर्फ़ SUCCESS पर चलाता है, तो echo A skip हो जाता है (कोई A नहीं)। फिर 'true || echo B' में, पहली command true SUCCEED होती है, और || दूसरी सिर्फ़ FAILURE पर चलाता है, तो echo B skip हो जाता है (कोई B नहीं)। तो न A न B print होता है। Option A ग़लत है: && को success चाहिए (जो false नहीं है) और || को failure चाहिए (जो true नहीं है), तो दोनों echos skip हो जाते हैं। Options C और D दोनों में से एक skip miss करते हैं। Rule: && success पर fire करता है, || failure पर, यहाँ पहली command का status हर echo को block करता है।

Think first

'Success' सारे numbers में से Zero से क्यों Represent होता है?

ज़्यादातर programming में, zero का मतलब false या nothing है। एक exit status का 0 success क्यों मतलब रखता है? फिर tap कीजिए।

Show the answer

क्योंकि एक exit status describe करता है एक command कैसे ख़त्म हुई, और fully succeed होने का exactly एक तरीका है पर fail होने के कई distinct तरीके हैं, तो zero 'no error' के लिए reserved है और बाकी numbers specific errors नाम देने के लिए free हैं। Exit status को एक error code की तरह सोचिए: 0 का मतलब है 'zero problems, सब कुछ fine गया', जबकि 1, 2, और बाकी non-zero values हर एक एक अलग तरह की failure मतलब रख सकते हैं (file not found, permission denied, bad arguments, वगैरह)। अगर success कोई non-zero number होता instead, आप एक value waste करते जो errors distinguish करने के लिए कहीं ज़्यादा useful है। यही वजह है यह boolean true/false से compared backwards लगता है, exit statuses की दुनिया में, सवाल 'true या false?' नहीं है बल्कि 'कितनी चीज़ें wrong गईं?', और best answer zero है। Logical operators directly इसी पर build करते हैं: && continue करता है जब problems का count zero है (success), और || step in करता है जब यह non-zero है (failure)। एक बार आप 'zero का मतलब है यह काम कर गया' internalise कर लें, $?, &&, और || सब click हो जाते हैं। Zero problems ही goal है।

Summary

Key takeaways

  • हर command एक exit status return करती है: 0 का मतलब success, कोई भी non-zero value का मतलब failure (echo $? से देखा जाता है)।
  • && (logical AND) अगली command सिर्फ़ तब चलाता है जब पिछली succeed हुई हो।
  • || (logical OR) अगली command सिर्फ़ तब चलाता है जब पिछली fail हुई हो।
  • इन्हें 'and then' (&&) और 'or else' (||) की तरह पढ़िए।
  • Idioms: mkdir dir && cd dir (बनने पर ही enter करो); command || echo 'failed' (failure पर fallback)।
  • Success zero है क्योंकि succeed होने का एक तरीका है पर fail होने के कई तरीके हैं, तो non-zero values errors नाम दे सकते हैं।
  • Memory hook: success पर &&, failure पर ||, और 0 का मतलब है यह काम कर गया।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Shell Scripting in Linux

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Logical Operators (&&, ||, !) · Linux Operating System (LOS) (Minor-04) · Gri-Learn