set -e (Error पर Exit) और इसकी Gotchas
In this page:
set -e
command_that_must_succeed
command_that_may_fail || true
Basic Exit-on-Error Behavior
set -e active होने पर, एक script चलना बंद कर देता है जैसे ही कोई command एक non-zero exit status return करे, बजाय silently बाद वाली lines में continue करने के जो उस command के succeed होने पर depend कर सकती हैं।
उदाहरण: Basic Exit-on-Error Behavior
#!/bin/bash
(
set -e
echo "step 1"
false
echo "step 2 - never reached"
) || echo "the subshell stopped early because of set -e"
Login to try C/C++/Java/PHP code in the editor
The if-condition Exemption
एक command जिसे if, while, या until की condition के रूप में test किया जा रहा है set -e से exempt है, क्योंकि वहां एक non-zero status expected और meaningful है, एक bug नहीं।
उदाहरण: The if-condition Exemption
#!/bin/bash
(
set -e
if false; then
echo "unreachable"
else
echo "set -e did NOT trigger here, because false was an if-condition"
fi
echo "the subshell kept running normally"
)
Login to try C/C++/Java/PHP code in the editor
The Pipeline Exemption
एक pipeline के अंदर, plain set -e सिर्फ LAST command के exit status पर react करता है, इसलिए एक earlier stage में failure silently ignore हो जाती है जब तक pipefail भी enabled न हो।
उदाहरण: The Pipeline Exemption
#!/bin/bash
(
set -e
false | true
echo "reached because only the last command (true) mattered to set -e"
)
(
set -e -o pipefail
false | true
echo "unreachable with pipefail active"
) || echo "with pipefail, the earlier failure was noticed and stopped the subshell"
Login to try C/C++/Java/PHP code in the editor
Catching a set -e Failure Deliberately
एक risky command को अपने खुद के set -e वाले एक subshell में wrap करना, और subshell के overall result को || से check करना, एक script को पूरे script को kill किए बिना एक strict failure demonstrate या contain करने देता है।
set -e behavior को एक large script पर भरोसा करने से पहले एक छोटे subshell में ऐसे test करें; इसकी exemptions trip करना आसान है।उदाहरण: Catching a set -e Failure Deliberately
#!/bin/bash
(set -e; false; echo "unreached") || echo "caught: the previous command failed under set -e"
echo "the main script keeps running normally"
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि
set -eहर possible failure catch करता है; यह एकif/whilecondition के अंदर, एक&&/||से पहले, या एक pipeline के किसी भी-but-last part के रूप में एक failing command के लिए trigger नहीं होता। - यह देखकर surprised होना कि एक condition में उपयोग किए गए एक function call के अंदर एक failing command के बाद भी script चलता रहता है, क्योंकि
set -eकी exemptions वहां भी apply होती हैं। - Robust error handling के लिए सिर्फ
set -eपर भरोसा करना बजाय इसे उन specific commands के लिए explicit checks के साथ combine करने के जो सबसे ज़्यादा matter करते हैं।
set -escript को तुरंत exit करता है अगर एक command non-zero exit हो, next line पर continue करने के बजाय।set -eif,while,until, या&&/||से पहले test की गई एक command पर APPLY नहीं होता — उनके potentially fail होने की expectation होती है।- एक pipeline में,
set -eसिर्फ LAST command के exit status पर react करता है, जब तकpipefailभी set न हो। set -eको ज़्यादा predictable strict-mode behavior के लिएset -uऔरset -o pipefailके साथ combine करना best है।
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