Script Input और Arguments को Validate करना
In this page:
if [[ $# -lt count ]]; then
echo "usage: $0 arguments" >&2
exit 1
fi
variable=${1:?usage message}
Argument Count Check करना
$# script (या function) को pass किए गए positional parameters की संख्या रखता है। इसे पहले से check करना missing arguments को बाद में confusing errors पैदा करने से पहले पकड़ लेता है।
उदाहरण: Checking the Argument Count
#!/bin/bash
set -- "only_one_arg"
if [[ $# -lt 2 ]]; then
echo "warning: expected 2 arguments, got $#" >&2
else
echo "got both arguments: $1 and $2"
fi
Login to try C/C++/Java/PHP code in the editor
:? से एक Value Require करना
${var:?message} normally var की value तक expand होता है, लेकिन अगर var unset या empty हो, यह message stderr पर print करता है और script को तुरंत exit करने का कारण बनता है, जो एक compact one-line required-argument check है।
उदाहरण: Requiring a Value with :?
#!/bin/bash
set -- "config.yaml"
file=${1:?usage: script.sh <filename>}
echo "processing file: $file"
Login to try C/C++/Java/PHP code in the editor
एक Value की Shape Validate करना
यह check करने के आगे कि एक value मौजूद है, scripts को यह confirm करना चाहिए कि इसकी expected shape है, जैसे purely numeric होना, arithmetic में इस पर rely करने से पहले एक regex match उपयोग करते हुए।
उदाहरण: Validating a Value's Shape
#!/bin/bash
validate_number() {
if [[ $1 =~ ^-?[0-9]+$ ]]; then
echo "$1 is a valid integer"
return 0
else
echo "'$1' is not a valid integer" >&2
return 1
fi
}
validate_number "42"
validate_number "abc" || echo "validation correctly rejected non-numeric input"
Login to try C/C++/Java/PHP code in the editor
Bad Input पर एक Usage Message
जब validation fail हो, stderr पर एक clear usage message print करना और non-zero exit करना caller (एक human या दूसरी script) को एक cryptic downstream failure के बजाय एक immediate, actionable explanation देता है।
उदाहरण: A Usage Message on Bad Input
#!/bin/bash
usage() {
echo "Usage: $0 <name> <count>" >&2
exit 1
}
set -- "widget"
if [[ $# -ne 2 ]]; then
( usage ) 2>/dev/null
echo "usage check failed as expected with $# argument(s)"
fi
Login to try C/C++/Java/PHP code in the editor
$1जैसे positional parameter को यह पहले check किए बिना उपयोग करना कि यह actually दिया गया था, जो clear message के बजाय confusing downstream errors की ओर ले जाता है।- यह validate करना कि argument non-empty है पर यह नहीं कि इसकी सही shape है (उदाहरण के लिए एक number blank नहीं है check करना पर यह नहीं कि यह actually numeric है)।
- Validation fail होने पर silently fail होना, एक clear usage message और non-zero exit code print करने के बजाय।
${1:?message}तुरंतmessageके साथ error देता है अगर$1unset या empty हो, बिना एक अलगifcheck की ज़रूरत के।$#positional parameters की संख्या रखता है, इसलिए specific ones उपयोग करने से पहले इसे check किया जा सकता है।- Regex या arithmetic checks एक value की shape confirm करते हैं (numeric, एक pattern से match करती है), सिर्फ यह नहीं कि यह non-empty है।
- एक अच्छा validation failure stderr पर एक usage message print करता है और non-zero exit करता है, bad data के साथ continue करने के बजाय।
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