In recent Korn Shell ksh93,
getopts can be more powerful than you think, it can :
- naturally parse short and long options with error management
- manage option value as optional, as mandatory or as numeric
- auto generate man page alike
This article will show how to use
getopts in recent ksh93. If you have older ksh, read this other article instead.
(foobar_getopts.ksh zip file attachement in this article)
This script has been tested on ...
... Oracle Linux 7
... /bin/ksh --version #sh (AT&T Research) 93u+ 2012-08-01
foobar_getopts.ksh source code
foobar_getopts.ksh is a script example to show how to use
getopts with short/long options in recent ksh93.
This script is also available for download at the end of the article.
How to define an option
In this script, options are defined in
#== script options ==# part with
SCRIPT_OPTS variable as follow:
| ||One letter for short option|
| ||String for long option|
| ||Option text description|
| ||Use |
| ||name of the value|
| ||value by default|
Extra description can be added within
} as it is done for
barfoo option :
How to display help and man
getopts has already some predefined options :
-?display short usage
--mandisplay full man page
And for some more specific information, these predefined options:
--aboutdisplay version, author,copyright and licence information
--versiondisplay version only
--authordisplay author only
--copyrightdisplay copyright only
--licensedisplay license only
getopts manage itself options errors :
getopts works without errors :
Better you test by yourself to see if it works and how it works.
Please leave comments and suggestions,
Enjoyed this article? Please like it or share it.