hrishy samant wrote:
>> hey thanks buddy can you also if possible tell me what
> is getopts all about.
"man getopts" tells you that it's a bash builtin (there may also a
getopts in section 3, which does something similar for C programs).
"man bash" followed by a search for getopts yields the passage below
(sorry for the long c&p):
There's an Advanced-Bash-Scripting-HOWTO which explains in practical
terms how to use this.
Basically 'while getopts "a:bc:" curr_option; do ... done' is the
general form. The colons mean that a and c expect arguments - this pops
one arg at a time off the argument stack and stores it in curr_option,
and for options expecting an argument, the option is put in $OPTARG. If
you preceed the options with a :, then a - is obligatory before the
If this isn't enough information for you, God help you ;-)
getopts optstring name [args]
getopts is used by shell procedures to parse posi
tional parameters. optstring contains the option
characters to be recognized; if a character is fol
lowed by a colon, the option is expected to have an
argument, which should be separated from it by
white space. The colon and question mark charac
ters may not be used as option characters. Each
time it is invoked, getopts places the next option
in the shell variable name, initializing name if it
does not exist, and the index of the next argument
to be processed into the variable OPTIND. OPTIND
is initialized to 1 each time the shell or a shell
script is invoked. When an option requires an
argument, getopts places that argument into the
variable OPTARG. The shell does not reset OPTIND
automatically; it must be manually reset between
multiple calls to getopts within the same shell
invocation if a new set of parameters is to be
When the end of options is encountered, getopts
exits with a return value greater than zero.
OPTIND is set to the index of the first non-option
argument, and name is set to ?.
getopts normally parses the positional parameters,
but if more arguments are given in args, getopts
parses those instead.
getopts can report errors in two ways. If the
first character of optstring is a colon, silent
error reporting is used. In normal operation diag
nostic messages are printed when invalid options or
missing option arguments are encountered. If the
variable OPTERR is set to 0, no error messages will
be displayed, even if the first character of opt
string is not a colon.
If an invalid option is seen, getopts places ? into
name and, if not silent, prints an error message
and unsets OPTARG. If getopts is silent, the
option character found is placed in OPTARG and no
diagnostic message is printed.
If a required argument is not found, and getopts is
not silent, a question mark (?) is placed in name,
OPTARG is unset, and a diagnostic message is
printed. If getopts is silent, then a colon (:) is
placed in name and OPTARG is set to the option
getopts returns true if an option, specified or
unspecified, is found. It returns false if the end
of options is encountered or an error occurs.
David Neary, E-Mail dave.neary at palamon.ie
Palamon Technologies Ltd. Phone +353-1-634-5059
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!