2 .\" Copyright 1989 AT&T
3 .\" Copyright (c) 2000, Sun Microsystems, Inc.
4 .\" All Rights Reserved
5 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
6 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
7 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
8 .TH GETOPTCVT 1 "Jan 7, 2000"
10 getoptcvt \- convert to getopts to parse command options
14 \fB/usr/lib/getoptcvt\fR [\fB-b\fR] \fIfilename\fR
19 \fB/usr/lib/getoptcvt\fR
25 \fB/usr/lib/getoptcvt\fR reads the shell script in \fIfilename\fR, converts it
26 to use \fBgetopts\fR instead of \fBgetopt\fR, and writes the results on the
30 \fBgetopts\fR is a built-in Bourne shell command used to parse positional
31 parameters and to check for valid options. See \fBsh\fR(1). It supports all
32 applicable rules of the command syntax standard (see Rules 3-10,
33 \fBIntro\fR(1)). It should be used in place of the \fBgetopt\fR command. (See
34 the NOTES section below.) The syntax for the shell's built-in \fBgetopts\fR
38 \fBgetopts\fR \fIoptstring\fR \fIname\fR [ \fIargument\fR\|.\|.\|.\|]
41 \fIoptstring\fR must contain the option letters the command using \fBgetopts\fR
42 will recognize; if a letter is followed by a colon (\fB:\fR), the option is
43 expected to have an argument, or group of arguments, which must be separated
44 from it by white space.
47 Each time it is invoked, \fBgetopts\fR places the next option in the shell
48 variable \fIname\fR and the index of the next argument to be processed in the
49 shell variable \fBOPTIND\fR. Whenever the shell or a shell script is invoked,
50 \fBOPTIND\fR is initialized to \fB1\fR.
53 When an option requires an option-argument, \fBgetopts\fR places it in the
54 shell variable \fBOPTARG\fR.
57 If an illegal option is encountered, \fB?\fR will be placed in \fIname\fR.
60 When the end of options is encountered, \fBgetopts\fR exits with a non-zero
61 exit status. The special option \fB \(mi\(mi\fR may be used to delimit the end
65 By default, \fBgetopts\fR parses the positional parameters. If extra arguments
66 (\fIargument\fR .\|.\|.) are given on the \fBgetopts\fR command line,
67 \fBgetopts\fR parses them instead.
70 So that all new commands will adhere to the command syntax standard described
71 in \fBIntro\fR(1), they should use \fBgetopts\fR or \fBgetopt\fR to parse
72 positional parameters and check for options that are valid for that command
73 (see the NOTES section below).
77 The following option is supported:
84 Makes the converted script portable to earlier releases of the UNIX system.
85 \fB/usr/lib/getoptcvt\fR modifies the shell script in \fIfilename\fR so that
86 when the resulting shell script is executed, it determines at run time whether
87 to invoke \fBgetopts\fR or \fBgetopt\fR.
92 \fBExample 1 \fRProcessing the arguments for a command
95 The following fragment of a shell program shows how one might process the
96 arguments for a command that can take the options \fB-a\fR or \fB-b\fR, as well
97 as the option \fB-o\fR, which requires an option-argument:
111 shift `expr $OPTIND \(mi 1`
116 \fBExample 2 \fREquivalent code expressions
119 This code accepts any of the following as equivalent:
124 \fBcmd -a -b -o "xxx z yy" filename
125 cmd -a -b -o "xxx z yy" -filename
126 cmd -ab -o xxx,z,yy filename
127 cmd -ab -o "xxx z yy" filename
128 cmd -o xxx,z,yy b a filename\fR
133 .SH ENVIRONMENT VARIABLES
136 See \fBenviron\fR(5) for descriptions of the following environment variables
137 that affect the execution of \fBgetopts\fR: \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR,
145 This variable is used by \fBgetoptcvt\fR as the index of the next argument to
155 This variable is used by \fBgetoptcvt\fR to store the argument if an option is
162 The following exit values are returned:
169 An option, specified or unspecified by \fIoptstring\fR, was found.
178 The end of options was encountered or an error occurred.
184 See \fBattributes\fR(5) for descriptions of the following attributes:
192 ATTRIBUTE TYPE ATTRIBUTE VALUE
199 \fBIntro\fR(1), \fBgetopts\fR(1), \fBsh\fR(1), \fBshell_builtins\fR(1),
200 \fBgetopt\fR(3C), \fBattributes\fR(5)
204 \fBgetopts\fR prints an error message on the standard error when it encounters
205 an option letter not included in \fIoptstring\fR.
209 Although the following command syntax rule (see \fBIntro\fR(1)) relaxations are
210 permitted under the current implementation, they should not be used because
211 they may not be supported in future releases of the system. As in the EXAMPLES
212 section above, \fB-a\fR and \fB-b\fR are options, and the option \fB-o\fR
213 requires an option-argument. The following example violates Rule 5: options
214 with option-arguments must not be grouped with other options:
218 example% \fBcmd -aboxxx filename\fR
225 The following example violates Rule 6: there must be white space after an
226 option that takes an option-argument:
230 example% \fBcmd -ab oxxx filename\fR
237 Changing the value of the shell variable \fBOPTIND\fR or parsing different sets
238 of arguments may lead to unexpected results.