getopts.html
来自「IEEE 1003.1-2003, Single Unix Specificat」· HTML 代码 · 共 384 行 · 第 1/2 页
HTML
384 行
<li><p>The invoking program name shall be identified in the message. The invoking program name shall be the value of the shell specialparameter 0 (see <a href="xcu_chap02.html#tag_02_05_02"><i>Special Parameters</i></a> ) at the time the <i>getopts</i> utility isinvoked. A name equivalent to:</p><pre><tt>basename "$0"</tt></pre><p>may be used.</p></li><li><p>If an option is found that was not specified in <i>optstring</i>, this error is identified and the invalid option charactershall be identified in the message.</p></li><li><p>If an option requiring an option-argument is found, but an option-argument is not found, this error shall be identified and theinvalid option character shall be identified in the message.</p></li></ul></blockquote><h4><a name="tag_04_62_12"></a>OUTPUT FILES</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_04_62_13"></a>EXTENDED DESCRIPTION</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_04_62_14"></a>EXIT STATUS</h4><blockquote><p>The following exit values shall be returned:</p><dl compact><dt> 0</dt><dd>An option, specified or unspecified by <i>optstring</i>, was found.</dd><dt>>0</dt><dd>The end of options was encountered or an error occurred.</dd></dl></blockquote><h4><a name="tag_04_62_15"></a>CONSEQUENCES OF ERRORS</h4><blockquote><p>Default.</p></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_04_62_16"></a>APPLICATION USAGE</h4><blockquote><p>Since <i>getopts</i> affects the current shell execution environment, it is generally provided as a shell regular built-in. Ifit is called in a subshell or separate utility execution environment, such as one of the following:</p><pre><tt>(getopts abc value "$@")nohup getopts ...find . -exec getopts ... \;</tt></pre><p>it does not affect the shell variables in the caller's environment.</p><p>Note that shell functions share <i>OPTIND</i> with the calling shell even though the positional parameters are changed. If thecalling shell and any of its functions uses <i>getopts</i> to parse arguments, the results are unspecified.</p></blockquote><h4><a name="tag_04_62_17"></a>EXAMPLES</h4><blockquote><p>The following example script parses and displays its arguments:</p><pre><tt>aflag=bflag=while getopts ab: namedo case $name in a) aflag=1;; b) bflag=1 bval="$OPTARG";; ?) printf "Usage: %s: [-a] [-b value] args\n" $0 exit 2;; esacdoneif [ ! -z "$aflag" ]; then printf "Option -a specified\n"fiif [ ! -z "$bflag" ]; then printf 'Option -b "%s" specified\n' "$bval"fishift $(($OPTIND - 1))printf "Remaining arguments are: %s\n" "$*"</tt></pre></blockquote><h4><a name="tag_04_62_18"></a>RATIONALE</h4><blockquote><p>The <i>getopts</i> utility was chosen in preference to the System V <i>getopt</i> utility because <i>getopts</i> handlesoption-arguments containing <blank>s.</p><p>The <i>OPTARG</i> variable is not mentioned in the ENVIRONMENT VARIABLES section because it does not affect the execution of<i>getopts</i>; it is one of the few "output-only" variables used by the standard utilities.</p><p>The colon is not allowed as an option character because that is not historical behavior, and it violates the Utility SyntaxGuidelines. The colon is now specified to behave as in the KornShell version of the <i>getopts</i> utility; when used as the firstcharacter in the <i>optstring</i> operand, it disables diagnostics concerning missing option-arguments and unexpected optioncharacters. This replaces the use of the <i>OPTERR</i> variable that was specified in an early proposal.</p><p>The formats of the diagnostic messages produced by the <i>getopts</i> utility and the <a href="../functions/getopt.html"><i>getopt</i>()</a> function are not fully specified because implementations with superior(``friendlier") formats objected to the formats used by some historical implementations. The standard developers considered itimportant that the information in the messages used be uniform between <i>getopts</i> and <a href="../functions/getopt.html"><i>getopt</i>()</a>. Exact duplication of the messages might not be possible, particularly if a utilityis built on another system that has a different <a href="../functions/getopt.html"><i>getopt</i>()</a> function, but the messagesmust have specific information included so that the program name, invalid option character, and type of error can be distinguishedby a user.</p><p>Only a rare application program intercepts a <i>getopts</i> standard error message and wants to parse it. Therefore,implementations are free to choose the most usable messages they can devise. The following formats are used by many historicalimplementations:</p><pre><tt>"%s: illegal option -- %c\n", <</tt><i>program name</i><tt>>, <</tt><i>option character</i><tt>><br>"%s: option requires an argument -- %c\n", <</tt><i>program name</i><tt>>, \ <</tt><i>option character</i><tt>></tt></pre><p>Historical shells with built-in versions of <a href="../functions/getopt.html"><i>getopt</i>()</a> or <i>getopts</i> have useddifferent formats, frequently not even indicating the option character found in error.</p></blockquote><h4><a name="tag_04_62_19"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_04_62_20"></a>SEE ALSO</h4><blockquote><p><a href="xcu_chap02.html#tag_02_05_02"><i>Special Parameters</i></a> , the System Interfaces volume ofIEEE Std 1003.1-2001, <a href="../functions/getopt.html"><i>getopt</i>()</a></p></blockquote><h4><a name="tag_04_62_21"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 4.</p></blockquote><h4><a name="tag_04_62_22"></a>Issue 6</h4><blockquote><p>The normative text is reworded to avoid use of the term "must" for application requirements.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX ® is a registered Trademark of The Open Group.<br>POSIX ® is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?