1getopt(1) User Commands getopt(1)
2
3
4
6 getopt - parse command options
7
9 set -- ` getopt optstring $ * `
10
11
13 The getopts command supersedes getopt. For more information, see NOTES
14 below.
15
16
17 getopt is used to break up options in command lines for easy parsing by
18 shell procedures and to check for legal options. optstring is a string
19 of recognized option letters; see getopt(3C). If a letter is followed
20 by a colon (:), the option is expected to have an argument which may or
21 may not be separated from it by white space. The special option - is
22 used to delimit the end of the options. If it is used explicitly,
23 getopt recognizes it; otherwise, getopt generates it; in either case,
24 getopt places it at the end of the options. The positional parameters
25 ($1 $2 ...) of the shell are reset so that each option is preceded by a
26 − and is in its own positional parameter; each option argument is also
27 parsed into its own positional parameter.
28
30 Example 1 Processing the arguments for a command
31
32
33 The following code fragment shows how one might process the arguments
34 for a command that can take the options -a or -b, as well as the option
35 -o, which requires an argument:
36
37
38 set -- `getopt abo: $*`
39 if [ $? != 0 ]
40 then
41 echo $USAGE
42 exit 2
43 fi
44 for i in $*
45 do
46 case $i in
47 -a | -b) FLAG=$i; shift;;
48 -o) OARG=$2; shift 2;;
49 --) shift; break;;
50 esac
51 done
52
53
54
55
56 This code accepts any of the following as equivalent:
57
58
59 cmd -aoarg filename1 filename2
60 cmd -a -o arg filename1 filename2
61 cmd -oarg -a filename1 filename2
62 cmd -a -oarg -- filename1 filename2
63
64
65
67 See attributes(5) for descriptions of the following attributes:
68
69
70
71
72 ┌─────────────────────────────┬─────────────────────────────┐
73 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
74 ├─────────────────────────────┼─────────────────────────────┤
75 │Availability │SUNWcsu │
76 │CSI │enabled │
77 └─────────────────────────────┴─────────────────────────────┘
78
80 Intro(1), getopts(1), getoptcvt(1), sh(1), shell_builtins(1),
81 getopt(3C), attributes(5)
82
84 getopt prints an error message on the standard error when it encounters
85 an option letter not included in optstring.
86
88 getopt will not be supported in the next major release. For this
89 release a conversion tool has been provided, namely, getoptcvt. For
90 more information, see getopts(1) and getoptcvt(1).
91
92
93 Reset optind to 1 when rescanning the options.
94
95
96 getopt does not support the part of Rule 8 of the command syntax stan‐
97 dard (see Intro(1)) that permits groups of option-arguments following
98 an option to be separated by white space and quoted. For example,
99
100 cmd -a -b -o "xxx z yy" filename
101
102
103
104
105 is not handled correctly. To correct this deficiency, use the getopts
106 command in place of getopt.
107
108
109 If an option that takes an option-argument is followed by a value that
110 is the same as one of the options listed in optstring (referring to the
111 earlier EXAMPLES section, but using the following command line:
112
113 cmd -o -a filename
114
115
116
117
118 getopt always treats it as an option-argument to -o; it never recog‐
119 nizes -a as an option. For this case, the for loop in the example
120 shifts past the filename argument.
121
122
123
124SunOS 5.11 7 Jan 2000 getopt(1)