getopts.html
来自「IEEE 1003.1-2003, Single Unix Specificat」· HTML 代码 · 共 384 行 · 第 1/2 页
HTML
384 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2003 The Open Group, All Rights Reserved --><title>getopts</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="getopts"></a> <a name="tag_04_62"></a><!-- getopts --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright © 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_04_62_01"></a>NAME</h4><blockquote>getopts - parse utility options</blockquote><h4><a name="tag_04_62_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><p><code><tt>getopts</tt> <i>optstring name</i> <b>[</b><i>arg</i><tt>...</tt><b>]</b></code></p></blockquote><h4><a name="tag_04_62_03"></a>DESCRIPTION</h4><blockquote><p>The <i>getopts</i> utility shall retrieve options and option-arguments from a list of parameters. It shall support the UtilitySyntax Guidelines 3 to 10, inclusive, described in the Base Definitions volume of IEEE Std 1003.1-2001, <a href="../basedefs/xbd_chap12.html#tag_12_02">Section 12.2, Utility Syntax Guidelines</a>.</p><p>Each time it is invoked, the <i>getopts</i> utility shall place the value of the next option in the shell variable specified bythe <i>name</i> operand and the index of the next argument to be processed in the shell variable <i>OPTIND .</i> Whenever the shellis invoked, <i>OPTIND</i> shall be initialized to 1.</p><p>When the option requires an option-argument, the <i>getopts</i> utility shall place it in the shell variable <i>OPTARG .</i> Ifno option was found, or if the option that was found does not have an option-argument, <i>OPTARG</i> shall be unset.</p><p>If an option character not contained in the <i>optstring</i> operand is found where an option character is expected, the shellvariable specified by <i>name</i> shall be set to the question-mark ( <tt>'?'</tt> ) character. In this case, if the firstcharacter in <i>optstring</i> is a colon ( <tt>':'</tt> ), the shell variable <i>OPTARG</i> shall be set to the option characterfound, but no output shall be written to standard error; otherwise, the shell variable <i>OPTARG</i> shall be unset and adiagnostic message shall be written to standard error. This condition shall be considered to be an error detected in the wayarguments were presented to the invoking application, but shall not be an error in <i>getopts</i> processing.</p><p>If an option-argument is missing:</p><ul><li><p>If the first character of <i>optstring</i> is a colon, the shell variable specified by <i>name</i> shall be set to the coloncharacter and the shell variable <i>OPTARG</i> shall be set to the option character found.</p></li><li><p>Otherwise, the shell variable specified by <i>name</i> shall be set to the question-mark character, the shell variable<i>OPTARG</i> shall be unset, and a diagnostic message shall be written to standard error. This condition shall be considered to bean error detected in the way arguments were presented to the invoking application, but shall not be an error in <i>getopts</i>processing; a diagnostic message shall be written as stated, but the exit status shall be zero.</p></li></ul><p>When the end of options is encountered, the <i>getopts</i> utility shall exit with a return value greater than zero; the shellvariable <i>OPTIND</i> shall be set to the index of the first non-option-argument, where the first <tt>"--"</tt> argument isconsidered to be an option-argument if there are no other non-option-arguments appearing before it, or the value <tt>"$#"</tt> +1if there are no non-option-arguments; the <i>name</i> variable shall be set to the question-mark character. Any of the followingshall identify the end of options: the special option <tt>"--"</tt> , finding an argument that does not begin with a <tt>'-'</tt> ,or encountering an error.</p><p>The shell variables <i>OPTIND</i> and <i>OPTARG</i> shall be local to the caller of <i>getopts</i> and shall not be exported bydefault.</p><p>The shell variable specified by the <i>name</i> operand, <i>OPTIND ,</i> and <i>OPTARG</i> shall affect the current shellexecution environment; see <a href="xcu_chap02.html#tag_02_12"><i>Shell Execution Environment</i></a> .</p><p>If the application sets <i>OPTIND</i> to the value 1, a new set of parameters can be used: either the current positionalparameters or new <i>arg</i> values. Any other attempt to invoke <i>getopts</i> multiple times in a single shell executionenvironment with parameters (positional parameters or <i>arg</i> operands) that are not the same in all invocations, or with an<i>OPTIND</i> value modified to be a value other than 1, produces unspecified results.</p></blockquote><h4><a name="tag_04_62_04"></a>OPTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_04_62_05"></a>OPERANDS</h4><blockquote><p>The following operands shall be supported:</p><dl compact><dt><i>optstring</i></dt><dd>A string containing the option characters recognized by the utility invoking <i>getopts</i>. If a character is followed by acolon, the option shall be expected to have an argument, which should be supplied as a separate argument. Applications shouldspecify an option character and its option-argument as separate arguments, but <i>getopts</i> shall interpret the charactersfollowing an option character requiring arguments as an argument whether or not this is done. An explicit null option-argument neednot be recognized if it is not supplied as a separate argument when <i>getopts</i> is invoked. (See also the <a href="../functions/getopt.html"><i>getopt</i>()</a> function defined in the System Interfaces volume of IEEE Std 1003.1-2001.)The characters question-mark and colon shall not be used as option characters by an application. The use of other option charactersthat are not alphanumeric produces unspecified results. If the option-argument is not supplied as a separate argument from theoption character, the value in <i>OPTARG</i> shall be stripped of the option character and the <tt>'-'</tt> . The first characterin <i>optstring</i> determines how <i>getopts</i> behaves if an option character is not known or an option-argument ismissing.</dd><dt><i>name</i></dt><dd>The name of a shell variable that shall be set by the <i>getopts</i> utility to the option character that was found.</dd></dl><p>The <i>getopts</i> utility by default shall parse positional parameters passed to the invoking shell procedure. If <i>arg</i>sare given, they shall be parsed instead of the positional parameters.</p></blockquote><h4><a name="tag_04_62_06"></a>STDIN</h4><blockquote><p>Not used.</p></blockquote><h4><a name="tag_04_62_07"></a>INPUT FILES</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_04_62_08"></a>ENVIRONMENT VARIABLES</h4><blockquote><p>The following environment variables shall affect the execution of <i>getopts</i>:</p><dl compact><dt><i>LANG</i></dt><dd>Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume ofIEEE Std 1003.1-2001, <a href="../basedefs/xbd_chap08.html#tag_08_02">Section 8.2, Internationalization Variables</a> forthe precedence of internationalization variables used to determine the values of locale categories.)</dd><dt><i>LC_ALL</i></dt><dd>If set to a non-empty string value, override the values of all the other internationalization variables.</dd><dt><i>LC_CTYPE</i></dt><dd>Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte asopposed to multi-byte characters in arguments and input files).</dd><dt><i>LC_MESSAGES</i></dt><dd>Determine the locale that should be used to affect the format and contents of diagnostic messages written to standarderror.</dd><dt><i>NLSPATH</i></dt><dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">Determine the location of message catalogs for the processing of <i>LC_MESSAGES .</i> <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt><i>OPTIND</i></dt><dd>This variable shall be used by the <i>getopts</i> utility as the index of the next argument to be processed.</dd></dl></blockquote><h4><a name="tag_04_62_09"></a>ASYNCHRONOUS EVENTS</h4><blockquote><p>Default.</p></blockquote><h4><a name="tag_04_62_10"></a>STDOUT</h4><blockquote><p>Not used.</p></blockquote><h4><a name="tag_04_62_11"></a>STDERR</h4><blockquote><p>Whenever an error is detected and the first character in the <i>optstring</i> operand is not a colon ( <tt>':'</tt> ), adiagnostic message shall be written to standard error with the following information in an unspecified format:</p><ul>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?