ksh getopts function


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 ;;
    esac
done
shift $(($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/ksh

USAGE="[+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 description
USAGE+="[i:ii]:[ivalue]"                    # -i, --ii with OPTARG
USAGE+="[c:cc]:[cvalue?description of c]"   # -c, --cc with OPTARG and description
USAGE+="[j:jj]:?[jvalue]"                   # -j, --jj with optional OPTARG
USAGE+="[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 description
USAGE+="[f:ff]#[fvalue]"           # -f, --ff with numeric OPTARG
USAGE+="[g:gg]#?[gvalue:=2]"       # -g, --gg with optional numeric OPTARG, default value


ivalue=
cvalue=
jvalue=
dvalue=ddefault
evalue=evalue
fvalue=
gvalue=2

OPTIND=1
while 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 ..." ;;
    esac
done
shift $((OPTIND-1))

#check at least one additional parameter is required.
if [ $# -eq 0 ]; then
    help
    exit 1
fi

#take actions for each parameters
for PARAM in $@; do
    do_action ${PARAM}
done

exit 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.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值