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

Script Input और Arguments को Validate करना

किसी script के उन values उपयोग करने से पहले जो किसी ने pass किए, उसे check करना चाहिए कि वे values actually सही दिख रही हैं, यह मान लेने के बजाय कि वे हमेशा सही होंगी।
Syntax
bash
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

bash
#!/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

:? से एक 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 :?

bash
#!/bin/bash
set -- "config.yaml"
file=${1:?usage: script.sh <filename>}
echo "processing file: $file"

एक Value की Shape Validate करना

यह check करने के आगे कि एक value मौजूद है, scripts को यह confirm करना चाहिए कि इसकी expected shape है, जैसे purely numeric होना, arithmetic में इस पर rely करने से पहले एक regex match उपयोग करते हुए।

उदाहरण: Validating a Value's Shape

bash
#!/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"

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

bash
#!/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
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. $1 जैसे positional parameter को यह पहले check किए बिना उपयोग करना कि यह actually दिया गया था, जो clear message के बजाय confusing downstream errors की ओर ले जाता है।
  2. यह validate करना कि argument non-empty है पर यह नहीं कि इसकी सही shape है (उदाहरण के लिए एक number blank नहीं है check करना पर यह नहीं कि यह actually numeric है)।
  3. Validation fail होने पर silently fail होना, एक clear usage message और non-zero exit code print करने के बजाय।
चैप्टर सारांश
  • ${1:?message} तुरंत message के साथ error देता है अगर $1 unset या empty हो, बिना एक अलग if check की ज़रूरत के।
  • $# 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 करने के बजाय।

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.