Command-Line Flags Parse करने के लिए getopts
In this page:
while getopts "optstring" option; do
case $option in
flag) commands ;;
option_with_arg) value=$OPTARG ;;
\?) usage ;;
esac
done
एक Basic Flag Loop
getopts "optstring" name एक while loop के अंदर बार-बार call किया जाता है; हर call positional parameters से अगला flag variable name (यहां opt) में parse करता है, सभी options consume हो जाने पर non-zero return करते हुए।
उदाहरण: A Basic Flag Loop
#!/bin/bash
set -- -v -f myfile.txt
while getopts "vf:" opt; do
case $opt in
v) echo "verbose mode on" ;;
f) echo "file argument: $OPTARG" ;;
esac
done
Login to try C/C++/Java/PHP code in the editor
वे Options जिन्हें एक Argument चाहिए
optstring में किसी letter के बाद एक colon, जैसे f:, getopts को बताता है कि -f के बाद एक value होनी चाहिए, जो getopts उस loop iteration के लिए $OPTARG में रखता है।
उदाहरण: Options That Require an Argument
#!/bin/bash
set -- -n "Ada" -c 3
while getopts "n:c:" opt; do
case $opt in
n) name=$OPTARG ;;
c) count=$OPTARG ;;
esac
done
echo "name=$name count=$count"
Login to try C/C++/Java/PHP code in the editor
Unknown और Missing Options Handle करना
optstring को एक colon से prefix करना (:vf:) getopts को silent error mode में switch करता है: एक unrecognized flag opt को ? पर set करता है, और अपना required argument missing वाला एक flag opt को : पर set करता है, दोनों को script explicitly handle कर सकती है getopts के अपने message print करने के बजाय।
उदाहरण: Handling Unknown and Missing Options
#!/bin/bash
set -- -x
while getopts ":vf:" opt; do
case $opt in
v) echo "verbose" ;;
f) echo "file: $OPTARG" ;;
\?) echo "unknown option: -$OPTARG" ;;
:) echo "option -$OPTARG requires an argument" ;;
esac
done
Login to try C/C++/Java/PHP code in the editor
Parsed Options से आगे Shift करना
getopts loop खत्म होने के बाद, OPTIND पहले non-option argument का index रखता है। shift $((OPTIND - 1)) सभी parsed flags हटा देता है, $@ को सिर्फ बाकी positional arguments पर set छोड़ते हुए।
उदाहरण: Shifting Past Parsed Options
#!/bin/bash
set -- -v file1.txt file2.txt
while getopts "v" opt; do
case $opt in
v) verbose=1 ;;
esac
done
shift $((OPTIND - 1))
echo "remaining arguments: $*"
Login to try C/C++/Java/PHP code in the editor
- option string में leading colon (जैसे
:vf:) भूल जाना getopts के default error messages को?और:cases के through manual handling के favor में disable करता है। - अगर
getoptssame shell session में एक से ज़्यादा बार उपयोग किया जाए (उदाहरण के लिए कई function calls में)OPTINDको 1 पर reset न करना, later calls को arguments skip करने का कारण बनते हुए। - एक argument लेने वाले option letter (जैसे
f:) के बाद colon भूल जाना, जो getopts को यह सोचने पर मजबूर करता है कि-fकोई argument नहीं लेता।
getopts "optstring" namepositional parameters से flags एक समय में एक, एक loop में parse करता है।- optstring में किसी letter के बाद एक colon (उदाहरण के लिए
f:) मतलब है कि उस option को एक argument चाहिए, जो$OPTARGमें store होता है। - optstring में एक leading colon silent error handling enable करता है: एक unknown option के लिए
?, एक missing required argument के लिए:। - Loop के बाद,
shift $((OPTIND - 1))parsed options हटा देता है, सिर्फ बाकी positional arguments छोड़ते हुए।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first:
- getopts for Parsing Command-Line Flags
- Positional Parameters and shift
- Process Substitution in Practice (diff <(...) <(...))
- Arrays of Arguments ("$@" vs "$*" Quoting Difference)
- Nameref Variables (declare -n) for Passing Arrays by Reference
- Heredoc Templating (Generating a Config File from Variables)
- Default/Fallback Argument Handling