Exit Status और $?
In this page:
command
echo "$?" # 0 = success, non-zero = failure
command1 && command2
command1 || command2
Command के बाद $? पढ़ना
किसी भी command के finish होने के बाद, Bash इसका exit status special variable $? में store करता है। आप immediately बाद में इसे inspect करके decide कर सकते हैं कि आपकी script को आगे क्या करना चाहिए, लेकिन अगला command run होते ही यह overwrite हो जाता है।
उदाहरण: Reading $? After a Command
#!/bin/bash
echo "hello"
echo "Exit status of echo: $?"
grep "zzz" <<< "no match here"
echo "Exit status of grep (1 = no match found): $?"
Login to try C/C++/Java/PHP code in the editor
Success बनाम Failure Codes
Exit status 0 conventionally मतलब है कि command succeed हुआ, जबकि 1 से 255 तक की कोई भी value failure signal करती है, भले ही हर non-zero number का exact meaning उस specific command द्वारा defined हो।
बहुत से commands सिर्फ 0 या 1 return करते हैं, लेकिन कुछ different तरह के errors distinguish करने के लिए अलग numbers उपयोग करते हैं।
उदाहरण: Success vs Failure Codes
#!/bin/bash
true
echo "true's exit status: $?"
false
echo "false's exit status: $?"
Login to try C/C++/Java/PHP code in the editor
Exit Status के आधार पर && और ||
&& एक दूसरे command को सिर्फ तब run होने के लिए chain करता है जब पहला status 0 (success) के साथ exit हुआ हो। || इसका opposite करता है, दूसरे command को सिर्फ तब run करता है जब पहला non-zero (failure) exit हुआ हो, जो simple conditional logic के लिए एक आम shorthand है।
उदाहरण: && and || Based on Exit Status
#!/bin/bash
mkdir -p /tmp/demo_$$ && echo "Directory created successfully"
grep "missing" <<< "nothing to find" || echo "Pattern not found, handled gracefully"
Login to try C/C++/Java/PHP code in the editor
आपकी Script का अपना Exit Status Set करना
एक script या function exit builtin के साथ एक number देकर explicitly वह exit status set कर सकता है जो callers देखेंगे। अगर exit को बिना argument के call किया जाए, script उस आखिरी command के status के साथ exit होती है जो इसने run किया था।
उदाहरण: Setting Your Script's Own Exit Status
#!/bin/bash
check_value=5
if [ "$check_value" -gt 0 ]; then
echo "Value is positive"
exit 0
fi
echo "This line would only run for non-positive values"
exit 1
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि एक non-zero exit status हमेशा एक crash का मतलब है; यह आमतौर पर सिर्फ command की failure की अपनी definition का मतलब है, जैसे
grepसिर्फ तब 1 return करता है जब इसे कोई matches न मिलें, जो कोई error condition नहीं है। - बीच में एक unrelated command, जैसे debugging के लिए
echo, run करने के बाद$?check करना;$?हमेशा *सबसे recently* completed command को reflect करता है, इसलिए आपके care वाले command के बाद run होने वाला कोई भी command इसे overwrite कर देता है। - यह भूल जाना कि exit status 0-255 पर capped है; बड़ी values इसके store होने के तरीके की वजह से wrap around होती हैं, जो surprising results produce कर सकता है।
$?सबसे recently executed command का exit status रखता है।- Convention के अनुसार, exit status
0मतलब success है, और कोई भी non-zero value (1-255) मतलब failure या एक specific error condition है। &&अगला command सिर्फ तब run करता है जब पिछला 0 exit हुआ हो;||अगला command सिर्फ तब run करता है जब पिछला non-zero exit हुआ हो।- एक script का अपना exit status explicitly
exitbuiltin से set किया जा सकता है, जैसेexit 1।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: