ksh getopts function
来源:互联网 发布:发型设计软件免费版 编辑:程序博客网 时间:2024/06/07 19:48
This is a sample on how to use getopts to parse command line options.
Principle
while getopts "ab:c" opt; do case $opt in a ) process option -a ;; b ) process option -b $OPTARG is the option's argument ;; c ) process option -c ;; \? ) print 'usage: bob [-a] [-b barg] [-c] args ...' exit 1 ;; esacdoneshift $(($OPTIND - 1))normal processing of arguments ...
- If a letter is followed by a ':', the option requires an argument.
- If a letter is followed by a '#', the option requires a numeric argument.
- The ':' or '#' may be followed by [description], i.e., a descriptive string enclosed in square brackets that is used when generating usage error messages.
- The leading ':',if the user types an invalid option, getopts normally prints an error message (of the form cmd: -o: unknown option) and sets opt to ?. getopts finishes processing all its options, and if an error was encountered, the shell exits. However -- now here's an obscure kludge -- if you begin the option letter string with a colon, getopts won't print the message, and shell will not exit. This allows you to handle error messages on your own.
while getopts ":ab:c" opt; do
Code Description
#!/bin/kshUSAGE="[+NAME?$0]"USAGE+="[+DESCRIPTION?this is program description]" # this two lines are used for unix man page style section headings USAGE+="[a:aa]" # -a, --aa USAGE+="[b:bb?description of b]" # -b, --bb with descriptionUSAGE+="[i:ii]:[ivalue]" # -i, --ii with OPTARGUSAGE+="[c:cc]:[cvalue?description of c]" # -c, --cc with OPTARG and descriptionUSAGE+="[j:jj]:?[jvalue]" # -j, --jj with optional OPTARGUSAGE+="[d:dd]:?[dvalue:=ddefault]" # -d, --dd with optional OPTARG, default value USAGE+="[e:ee]:?[evalue:=edefault?description of e]" # -e, --ee with optional OPTARG, default value, and descriptionUSAGE+="[f:ff]#[fvalue]" # -f, --ff with numeric OPTARGUSAGE+="[g:gg]#?[gvalue:=2]" # -g, --gg with optional numeric OPTARG, default valueivalue=cvalue=jvalue=dvalue=ddefaultevalue=evaluefvalue=gvalue=2OPTIND=1while getopts "$USAGE" opt; do case $opt in a) echo "in a, OPTARG=${OPTARG}" ;; b) echo "in b, OPTARG=${OPTARG}" ;; i) echo "in i, OPTARG=${OPTARG}" ;; c) echo "in c, OPTARG=${OPTARG}" ;; j) echo "in j, OPTARG=${OPTARG}" ;; d) echo "in d, OPTARG=${OPTARG}" ;; e) echo "in e, OPTARG=${OPTARG}" ;; f) echo "in f, OPTARG=${OPTARG}" ;; g) echo "in g, OPTARG=${OPTARG}" ;; \?) echo "Unknow Option ..." ;; esacdoneshift $((OPTIND-1))#check at least one additional parameter is required.if [ $# -eq 0 ]; then help exit 1fi#take actions for each parametersfor PARAM in $@; do do_action ${PARAM}doneexit 0
Usage Output
Input an invalid option
$ ./t.ksh -n
./t.ksh: -n: unknown option
Unknow Option ...
Usage: ./t.ksh [-ab] [-i ivalue] [-c cvalue] [-j[jvalue]] [-d[dvalue]] [-e[evalue]] [-f fvalue] [-g[gvalue]]
Show help information
$ ./t.ksh -?
Usage: ./t.ksh [-ab] [-i ivalue] [-c cvalue] [-j[jvalue]] [-d[dvalue]] [-e[evalue]] [-f fvalue] [-g[gvalue]]
Show long-description help information
$ ./t.ksh --help
Usage: ./t.ksh [ options ]
OPTIONS
-a, --aa
-b, --bb description of b
-i, --ii=ivalue
-c, --cc=cvalue description of c
-j, --jj[=jvalue] The option value may be omitted.
-d, --dd[=dvalue] The option value may be omitted. The default value is ddefault.
-e, --ee[=evalue]
description of e The option value may be omitted. The default value is edefault.
-f, --ff=fvalue
-g, --gg[=gvalue] The option value may be omitted. The default value is 2.
- ksh getopts function
- getopts
- getopts
- getopts
- ksh
- ksh
- getopts命令
- 使用getopts
- shell getopts
- shell-----getopts
- getopts 命令
- shell--getopts
- getopt getopts
- getopts详解
- getopts用法
- getopts 命令
- getopts使用
- ksh学习
- 工作总结
- Windows内存清理工具实现——从现象到本质
- 2016年淘宝购买华硕FL5900UQ7500惨痛经历教训总结
- 牛客网刷题之复杂链表的复制
- 【C语言】”student a am i “和“we are happy”
- ksh getopts function
- 线性代数阶段性总结
- android 跑马灯效果的相关补充
- Zynq动态更新FPGA比特流
- ksh control statement
- 快速排序(C语言)
- ksh array variable
- 问题积累一
- 蚂蚁金服11.11:支付宝和蚂蚁花呗的技术架构及实践