Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
howto:getopts_tutorial [2015/02/14 22:18] ormaaj [Used variables] OPTERR is bash-specific and not POSIX |
howto:getopts_tutorial [2018/03/20 23:57] ffox8 [See also] link |
||
---|---|---|---|
Line 4: | Line 4: | ||
===== Description ===== | ===== Description ===== | ||
- | + | **Note that** ''getopts'' is neither able to parse GNU-style long options (''<nowiki>--</nowiki>myoption'') nor XF86-style long options (''-myoption''). So, when you want to parse command line arguments in a professional ;-) way, ''getopts'' may or may not work for you. Unlike its older brother ''getopt'' (note the missing //s//!), it's a shell builtin command. The advantages are: | |
- | When you want to parse command line arguments in a professional way, ''getopts'' is the tool of choice. Unlike its older brother ''getopt'' (note the missing //s//!), it's a shell builtin command. The advantages are: | + | |
* No need to pass the positional parameters through to an external program. | * No need to pass the positional parameters through to an external program. | ||
* Being a builtin, ''getopts'' can set shell variables to use for parsing (impossible for an //external// process!) | * Being a builtin, ''getopts'' can set shell variables to use for parsing (impossible for an //external// process!) | ||
Line 11: | Line 10: | ||
* ''getopts'' is defined in POSIX(r). | * ''getopts'' is defined in POSIX(r). | ||
- | Some other methods to parse positional parameters (without ''getopt(s)'') are described in: [[scripting:posparams | How to handle positional parameters]]. | + | ---- |
+ | |||
+ | Some other methods to parse positional parameters - using neither **getopt** nor **getopts** - are described in: [[scripting:posparams | How to handle positional parameters]]. | ||
- | **Note that** ''getopts'' is not able to parse GNU-style long options (''<nowiki>--</nowiki>myoption'') or XF86-style long options (''-myoption'')! | ||
==== Terminology ==== | ==== Terminology ==== | ||
Line 24: | Line 24: | ||
These are all positional parameters, but they can be divided into several logical groups: | These are all positional parameters, but they can be divided into several logical groups: | ||
* ''-x'' is an **option** (aka **flag** or **switch**). It consists of a dash (''-'') followed by **one** character. | * ''-x'' is an **option** (aka **flag** or **switch**). It consists of a dash (''-'') followed by **one** character. | ||
- | * ''-f'' is also an option, but this option has an associated **option argument** (an argument to the option ''-f''): ''/etc/mybackup.conf''. The option argument is usually the argument following the option itself, but that isn't mandatory. Joining the option an option argument into a single argument ''-f/etc/mybackup.conf'' is valid. | + | * ''-f'' is also an option, but this option has an associated **option argument** (an argument to the option ''-f''): ''/etc/mybackup.conf''. The option argument is usually the argument following the option itself, but that isn't mandatory. Joining the option and option argument into a single argument ''-f/etc/mybackup.conf'' is valid. |
* ''-r'' depends on the configuration. In this example, ''-r'' doesn't take arguments so it's a standalone option like ''-x''. | * ''-r'' depends on the configuration. In this example, ''-r'' doesn't take arguments so it's a standalone option like ''-x''. | ||
* ''./foo.txt'' and ''./bar.txt'' are remaining arguments without any associated options. These are often used as **mass-arguments**. For example, the filenames specified for ''cp(1)'', or arguments that don't need an option to be recognized because of the intended behavior of the program. POSIX(r) calls them **operands**. | * ''./foo.txt'' and ''./bar.txt'' are remaining arguments without any associated options. These are often used as **mass-arguments**. For example, the filenames specified for ''cp(1)'', or arguments that don't need an option to be recognized because of the intended behavior of the program. POSIX(r) calls them **operands**. | ||
Line 296: | Line 296: | ||
* Internal: [[syntax:ccmd:while_loop]] | * Internal: [[syntax:ccmd:while_loop]] | ||
* POSIX [[http://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html#tag_20_54|getopts(1)]] and [[http://pubs.opengroup.org/onlinepubs/9699919799/functions/getopt.html|getopt(3)]] | * POSIX [[http://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html#tag_20_54|getopts(1)]] and [[http://pubs.opengroup.org/onlinepubs/9699919799/functions/getopt.html|getopt(3)]] | ||
+ | * [[https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash| parse CLI ARGV ]] |