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.
本文介绍如何利用getopt工具解析命令行选项。通过示例代码展示了不同类型的选项及其用法,包括必选参数、可选参数及数值参数等,并提供错误处理方式。
5889

被折叠的 条评论
为什么被折叠?



