← Back to Bash Course | Chapter 12: Error Handling & Debugging | Lesson 1 of 12

Exit Codes और $? को सही तरीके से Check करना

हर command खत्म होने पर एक number report करता है, जिसे exit code कहा जाता है, जहां zero का मतलब success है और कुछ भी और किसी तरह की failure का मतलब है।
Syntax
bash
command
status=$?
if command; then
  commands
fi
exit status_code

Reading $? Immediately

$? last command का exit status रखता है जो run हुआ था, और यह instant दूसरी कोई command run होती है, उसी पल overwrite हो जाता है, echo जैसी कुछ innocuous चीज़ भी शामिल। अगर आपको बाद में इसकी ज़रूरत है तो इसे तुरंत एक variable में capture करें।

उदाहरण: Reading $? Immediately

bash
#!/bin/bash
true
status=$?
echo "true exited with: $status"

false
status=$?
echo "false exited with: $status"

Testing a Command Directly

एक command run करने और फिर $? inspect करने के बजाय, command को सीधे एक if या while condition के अंदर रखना simpler और safer है, क्योंकि bash इसका exit status automatically evaluate करता है।

उदाहरण: Testing a Command Directly

bash
#!/bin/bash
if grep -q "root" <<< "root:x:0:0"; then
    echo "pattern matched, no need to check \$? separately"
fi

Setting a Script's Own Exit Code

exit N script को immediately रोकता है और इसका exit status N set करता है, जिसे कोई भी caller (दूसरा script, या shell) बाद में check कर सकता है। बिना explicit exit के script के आखिर तक पहुंचना last command का status उपयोग करता है।

उदाहरण: Setting a Script's Own Exit Code

bash
#!/bin/bash
check_even() {
    if (( $1 % 2 == 0 )); then
        return 0
    else
        return 1
    fi
}

if check_even 4; then
    echo "4 is even"
fi
if ! check_even 5; then
    echo "5 is not even"
fi

Exit Codes Beyond 0 and 1

Exit codes 0 से 255 तक कोई भी value हो सकते हैं, और कई real tools specific numbers का उपयोग specific चीज़ों के लिए करते हैं (उदाहरण के लिए, grep 2 का मतलब एक असली error occur हुई रखता है, 1 से अलग जिसका मतलब है "कोई match नहीं मिला")।

उदाहरण: Exit Codes Beyond 0 and 1

bash
#!/bin/bash
grep "x" /no/such/file 2>/dev/null
ecode=$?
if [[ $ecode -eq 2 ]]; then
    echo "grep reported an error (code 2), not just a non-match"
fi
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. वह command जिसकी आपको चिंता है और $? check करने के बीच दूसरी command run करना, जो $? को नई command के अपने status से overwrite कर देता है।
  2. यह मान लेना कि एक non-zero exit code हमेशा सिर्फ 0/1 का मतलब होता है; commands कोई भी value 0-255 return कर सकते हैं, हर एक का tool-specific meaning।
  3. $? को एक if के अंदर success check करने के लिए उपयोग करना जब आप command को सीधे if condition में रख सकते थे, जो simpler है और overwrite trap से पूरी तरह बचता है।
चैप्टर सारांश
  • Exit code 0 का मतलब success है; कोई भी non-zero value (1-255) किसी तरह की failure का मतलब है, meaning command-specific।
  • $? सबसे recently executed command का exit status रखता है और हर बाद वाली command द्वारा overwrite हो जाता है।
  • Command को जब possible हो if/while conditions के अंदर सीधे रखें बजाय बाद में $? check करने के।
  • Script के अंदर exit N उस script का अपना exit code set करता है, जिसे इसका caller फिर check कर सकता है।

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.