1getoptcvt(1) User Commands getoptcvt(1)
2
3
4
6 getoptcvt - convert to getopts to parse command options
7
9 /usr/lib/getoptcvt [-b] filename
10
11
12 /usr/lib/getoptcvt
13
14
16 /usr/lib/getoptcvt reads the shell script in filename, converts it to
17 use getopts instead of getopt, and writes the results on the standard
18 output.
19
20
21 getopts is a built-in Bourne shell command used to parse positional
22 parameters and to check for valid options. See sh(1). It supports all
23 applicable rules of the command syntax standard (see Rules 3-10,
24 Intro(1)). It should be used in place of the getopt command. (See the
25 NOTES section below.) The syntax for the shell's built-in getopts com‐
26 mand is:
27
28
29 getopts optstring name [ argument...]
30
31
32 optstring must contain the option letters the command using getopts
33 will recognize; if a letter is followed by a colon (:), the option is
34 expected to have an argument, or group of arguments, which must be sep‐
35 arated from it by white space.
36
37
38 Each time it is invoked, getopts places the next option in the shell
39 variable name and the index of the next argument to be processed in the
40 shell variable OPTIND. Whenever the shell or a shell script is invoked,
41 OPTIND is initialized to 1.
42
43
44 When an option requires an option-argument, getopts places it in the
45 shell variable OPTARG.
46
47
48 If an illegal option is encountered, ? will be placed in name.
49
50
51 When the end of options is encountered, getopts exits with a non-zero
52 exit status. The special option −− may be used to delimit the end of
53 the options.
54
55
56 By default, getopts parses the positional parameters. If extra argu‐
57 ments (argument ...) are given on the getopts command line, getopts
58 parses them instead.
59
60
61 So that all new commands will adhere to the command syntax standard
62 described in Intro(1), they should use getopts or getopt to parse posi‐
63 tional parameters and check for options that are valid for that command
64 (see the NOTES section below).
65
67 The following option is supported:
68
69 -b Makes the converted script portable to earlier releases of the
70 UNIX system. /usr/lib/getoptcvt modifies the shell script in
71 filename so that when the resulting shell script is executed, it
72 determines at run time whether to invoke getopts or getopt.
73
74
76 Example 1 Processing the arguments for a command
77
78
79 The following fragment of a shell program shows how one might process
80 the arguments for a command that can take the options -a or -b, as well
81 as the option -o, which requires an option-argument:
82
83
84 while getopts abo: c
85 do
86 case $c in
87 a | b) FLAG=$c;;
88 o) OARG=$OPTARG;;
89 \?) echo $USAGE
90 exit 2;;
91 esac
92 done
93 shift `expr $OPTIND − 1`
94
95
96 Example 2 Equivalent code expressions
97
98
99 This code accepts any of the following as equivalent:
100
101
102 cmd -a -b -o "xxx z yy" filename
103 cmd -a -b -o "xxx z yy" -filename
104 cmd -ab -o xxx,z,yy filename
105 cmd -ab -o "xxx z yy" filename
106 cmd -o xxx,z,yy b a filename
107
108
109
111 See environ(5) for descriptions of the following environment variables
112 that affect the execution of getopts: LC_CTYPE, LC_MESSAGES, and
113 NLSPATH.
114
115 OPTIND This variable is used by getoptcvt as the index of the next
116 argument to be processed.
117
118
119 OPTARG This variable is used by getoptcvt to store the argument if
120 an option is using arguments.
121
122
124 The following exit values are returned:
125
126 0 An option, specified or unspecified by optstring, was found.
127
128
129 >0 The end of options was encountered or an error occurred.
130
131
133 See attributes(5) for descriptions of the following attributes:
134
135
136
137
138 ┌─────────────────────────────┬─────────────────────────────┐
139 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
140 ├─────────────────────────────┼─────────────────────────────┤
141 │Availability │SUNWcsu │
142 │CSI │enabled │
143 └─────────────────────────────┴─────────────────────────────┘
144
146 Intro(1), getopts(1), sh(1), shell_builtins(1), getopt(3C),
147 attributes(5)
148
150 getopts prints an error message on the standard error when it encoun‐
151 ters an option letter not included in optstring.
152
154 Although the following command syntax rule (see Intro(1)) relaxations
155 are permitted under the current implementation, they should not be used
156 because they may not be supported in future releases of the system. As
157 in the EXAMPLES section above, -a and -b are options, and the option -o
158 requires an option-argument. The following example violates Rule 5:
159 options with option-arguments must not be grouped with other options:
160
161 example% cmd -aboxxx filename
162
163
164
165
166 The following example violates Rule 6: there must be white space after
167 an option that takes an option-argument:
168
169 example% cmd -ab oxxx filename
170
171
172
173
174 Changing the value of the shell variable OPTIND or parsing different
175 sets of arguments may lead to unexpected results.
176
177
178
179SunOS 5.11 7 Jan 2000 getoptcvt(1)