Small getopts tutorial

Small getopts tutorial

FIXME incomplete

When you want to parse commandline 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 advantage is

  • you don't need to hand your positional parameters through to an external program
  • getopts can easily set shell variables you can use for parsing (impossible for an external process!)
  • you don't have to argue with several getopt implementations which had buggy concepts in the past (whitespaces, …)
  • getopts is defined in POSIX®

Some other methods to parse positional parameters (without getopt(s)) are described in: How to handle positional parameters.

Note that getopts is not able to parse GNU-style long options (--myoption) or XF86-style long options (-myoption)!

Description

Terminology

It's useful to know what we're talking about here, so let's see… Consider the following commandline:

mybackup -x -f /etc/mybackup.conf -r ./foo.txt ./bar.txt
All these are positional parameters, but you can divide them into some logical groups:

  • -x is an option, a flag, a switch: one character, indroduced by a dash ( -)
  • -f is also an option, but this option has an additional argument (argument to the option -f): /etc/mybackup.conf. This argument is usually separated from its option (by a whitespace or any other splitting character) but that's not a must, -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
  • ./foo.txt and ./bar.txt are remaining arguments without any option related. These often are used as mass-arguments (like for example the filenames you specify for cp(1)) or for arguments that don't need an option to be recognized because of the intended behaviour of the program (like the filename argument you give your text-editor to open and display - why would one need an extra switch for that?). POSIX® calls them operands.

To give you an idea about why getopts is useful: The above commandline could also read like…

mybackup -xrf /etc/mybackup.conf ./foo.txt ./bar.txt
…which is very hard to parse by own code. getopts recognized all the common option formats.

The option flags can be upper- and lowercase characters, and of course digits. It may recognize other characters, but that's not recommended (usability and maybe problems with special characters).

How it works

In general you need to call getopts several times. Each time it will use "the next" positional parameter (and a possible argument), if parsable, and provide it to you. getopts will not change the positional parameter set — if you want to shift it, you have to do it manually after processing:

shift $((OPTIND-1))
# now do something with $@

Since getopts will set an exit status of FALSE when there's nothing left to parse, it's easy to use it in a while-loop:

while getopts ...; do
  ...
done

getopts will parse options and their possible arguments. It will stop parsing on the first non-option argument (a string that doesn't begin with  a hyphen (-) that isn't an argument for any option infront of it). It will also stop parsing when it sees the -- (double-hyphen), which means end of options.

Used variables

variabledescription
OPTINDHolds the index to the next argument to be processed. This is how getopts "remembers" its own status between invocations. Also usefull to shift the positional parameters after processing with getopts. OPTIND is initially set to 1, and needs to be re-set to 1 if you want to parse anything again with getopts
OPTARGThis variable is set to any argument for an option found by getopts. It also contains the option flag of an unknown option.
OPTERR(Values 0 or 1) Indicates if Bash should display error messages generated by the getopts builtin. The value is initialized to 1 on every shell startup - so be sure to always set it to 0 if you don't want to see annoying messages!

getopts also uses these variables for error reporting (they're set to value-combinations which arent possible in normal operation).

Specify what you want

The base-syntax for getopts is:

getopts OPTSTRING VARNAME [ARGS...]
where:

OPTSTRINGtells getopts which options to expect and where to expect arguments (see below)
VARNAMEtells getopts which shell-variable to use for option reporting
ARGStells getopts to parse these optional words instead of the positional parameters
The option-string

The option-string tells getopts which options to expect and which of them must have an argument. The syntax is very simple — every option character is simply named as is, this example-string would tell getopts to look for -f, -A and -x:

getopts fAx VARNAME

When you want getopts to expect an argument for an option, just place a : (colon) after the proper option flag. If you want -A to expect an argument (i.e. to become -A SOMETHING) just do:

getopts fA:x VARNAME

If the very first character of the option-string is a : (colon), which normally would be nonsense because there's no option letter preceeding it, getopts switches to the mode "silent error reporting". In productive scripts, this is usually what you want (handle errors yourself and don't get disturbed by annoying messages).

Custom arguments to parse

The getopts utility parses the positional parameters of the current shell or function by default (which means it parses "$@").

You can give your own set of arguments to the utility to parse. Whenever additional arguments are given after the VARNAME parameter, getopts doesn't try to parse the positional parameters, but these given words.

This way, you are able to parse any option set you like, here for example from an array:

while getopts :f:h opt "${MY_OWN_SET[@]}"; do
  ...
done

A call to getopts without these additional arguments is equivalent to explicitly calling it with "$@":

getopts ... "$@"

Error Reporting

Regarding error-reporting, there are two modes getopts can run in:

  • verbose mode
  • silent mode

For productive scripts I recommend to use the silent mode, since everything looks more professional, when you don't see annoying standard messages. Also it's easier to handle, since the failure cases are indicated in an easier way.

Verbose Mode
invalid optionVARNAME is set to ? (quersion-mark) and OPTARG is unset
required argument not foundVARNAME is set to ? (quersion-mark), OPTARG is unset and an error message is printed
Silent Mode
invalid optionVARNAME is set to ? (question-mark) and OPTARG is set to the (invalid) option character
required argument not foundVARNAME is set to : (colon) and OPTARG contains the option-character in question

Using it

A first example

Enough said - action!

Let's play with a very simple case: Only one option (-a) expected, without any arguments. Also we disable the verbose error handling by preceeding the whole option string with a colon (:):

#!/bin/bash
 
while getopts ":a" opt; do
  case $opt in
    a)
      echo "-a was triggered!" >&2
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      ;;
  esac
done
I put that into a file named go_test.sh, which is the name you'll see below in the examples.

Let's do some tests:

Calling it without any arguments

$ ./go_test.sh
$ 
Nothing happened? Right. getopts didn't see any valid or invalid options (letters preceeded by a dash), so it wasn't triggered.

Calling it with non-option arguments

$ ./go_test.sh /etc/passwd
$ 
Again — nothing happened. The very same case: getopts didn't see any valid or invalid options (letters preceeded by a dash), so it wasn't triggered.

The arguments given to your script are of course accessible as $1 - ${N}.

Calling it with option-arguments

Now let's trigger getopts: Provide options.

First, an invalid one:

$ ./go_test.sh -b
Invalid option: -b
$ 
As expected, getopts didn't accept this option and acted like told above: It placed ? into $opt and the invalid option character ( b) into $OPTARG. With our case statement, we were able to detect this.

Now, a valid one (-a):

$ ./go_test.sh -a
-a was triggered!
$ 
You see, the detection works perfectly. The a was put into the variable $opt for our case statement.

Of course it's possible to mix valid and invalid options when calling:

$ ./go_test.sh -a -x -b -c
-a was triggered!
Invalid option: -x
Invalid option: -b
Invalid option: -c
$ 

Finally, it's of course possible, to give our option multiple times:

$ ./go_test.sh -a -a -a -a
-a was triggered!
-a was triggered!
-a was triggered!
-a was triggered!
$ 

The last examples lead us to some points you may consider:

  • invalid options don't stop the processing: If you want to stop the script, you have to do it yourself ( exit in the right place)
  • multiple identical options are possible: If you want to disallow these, you have to check manually (e.g. by setting a variable or so)

An option with argument

Let's extend our example from above. Just a little bit:

  • -a now takes an argument
  • on an error, the parsing exits with exit 1

#!/bin/bash
 
while getopts ":a:" opt; do
  case $opt in
    a)
      echo "-a was triggered, Parameter: $OPTARG" >&2
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done

Let's do the very same tests we did in the last example:

Calling it without any arguments

$ ./go_test.sh
$ 
As above, nothing happend. It wasn't triggered.

Calling it with non-option arguments

$ ./go_test.sh /etc/passwd
$ 
The very same case: It wasn't triggered.

Calling it with option-arguments

Invalid option:

$ ./go_test.sh -b
Invalid option: -b
$ 
As expected, as above, getopts didn't accept this option and acted like programmed.

Valid option, but without the mandatory argument:

$ ./go_test.sh -a
Option -a requires an argument.
$ 
The option was okay, but there is an argument missing.

Let's provide the argument:

$ ./go_test.sh -a /etc/passwd
-a was triggered, Parameter: /etc/passwd
$

See also

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
牙科就诊管理系统利用当下成熟完善的SSM框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的Mysql数据库进行程序开发。实现了用户在线查看数据。管理员管理病例管理、字典管理、公告管理、药单管理、药品管理、药品收藏管理、药品评价管理、药品订单管理、牙医管理、牙医收藏管理、牙医评价管理、牙医挂号管理、用户管理、管理员管理等功能。牙科就诊管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 管理员在后台主要管理病例管理、字典管理、公告管理、药单管理、药品管理、药品收藏管理、药品评价管理、药品订单管理、牙医管理、牙医收藏管理、牙医评价管理、牙医挂号管理、用户管理、管理员管理等。 牙医列表页面,此页面提供给管理员的功能有:查看牙医、新增牙医、修改牙医、删除牙医等。公告信息管理页面提供的功能操作有:新增公告,修改公告,删除公告操作。公告类型管理页面显示所有公告类型,在此页面既可以让管理员添加新的公告信息类型,也能对已有的公告类型信息执行编辑更新,失效的公告类型信息也能让管理员快速删除。药品管理页面,此页面提供给管理员的功能有:新增药品,修改药品,删除药品。药品类型管理页面,此页面提供给管理员的功能有:新增药品类型,修改药品类型,删除药品类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值