Exit Codes और $? को सही तरीके से Check करना
In this page:
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
#!/bin/bash
true
status=$?
echo "true exited with: $status"
false
status=$?
echo "false exited with: $status"
Login to try C/C++/Java/PHP code in the editor
Testing a Command Directly
एक command run करने और फिर $? inspect करने के बजाय, command को सीधे एक if या while condition के अंदर रखना simpler और safer है, क्योंकि bash इसका exit status automatically evaluate करता है।
उदाहरण: Testing a Command Directly
#!/bin/bash
if grep -q "root" <<< "root:x:0:0"; then
echo "pattern matched, no need to check \$? separately"
fi
Login to try C/C++/Java/PHP code in the editor
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
#!/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
Login to try C/C++/Java/PHP code in the editor
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
#!/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
Login to try C/C++/Java/PHP code in the editor
- वह command जिसकी आपको चिंता है और
$?check करने के बीच दूसरी command run करना, जो$?को नई command के अपने status से overwrite कर देता है। - यह मान लेना कि एक non-zero exit code हमेशा सिर्फ 0/1 का मतलब होता है; commands कोई भी value 0-255 return कर सकते हैं, हर एक का tool-specific meaning।
$?को एकifके अंदर success check करने के लिए उपयोग करना जब आप command को सीधेifcondition में रख सकते थे, जो 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/whileconditions के अंदर सीधे रखें बजाय बाद में$?check करने के। - Script के अंदर
exit Nउस script का अपना exit code set करता है, जिसे इसका caller फिर check कर सकता है।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first:
- Exit Codes and Checking $? Correctly
- set -e and set -u
- Error Handling
- set -e (Exit on Error) and Its Gotchas
- Logging
- set -u (Undefined Variables) and set -x (Trace Mode)
- Script Arguments with getopts
- trap ERR for Custom Error Handling
- Debug Mode (set -x)
- Writing a Custom Error/die Function
- Portability
- Validating Script Input and Arguments