1823057 Members
3160 Online
109645 Solutions
New Discussion юеВ

getopt

 
SOLVED
Go to solution
itai weisman
Super Advisor

getopt

Hello ppl,
on unix shell scripting (bash, ksh, sh)
does anyone know how can I use getopt in order to examine command line parameters?
I couldn't find any helpfull info on man pages or other places on the web...
thanks
Itai.
10 REPLIES 10
Steven E. Protter
Exalted Contributor

Re: getopt

Shalom,

Example:

while getopts d:n:f:r:l: opt
do
case "$opt" in
d) dflg=1
DELAY=$OPTARG;;
r) rflg=1
RUNS=$OPTARG;;
n) nflg=1
NUM=$OPTARG;;
l) lflg=1
LF=$OPTARG;;
f) nflg=1
FILTER=$OPTARG;;
?) out_help
exit ;;
esac
done 2>/dev/null


http://www.hpuxconsulting.com/mem.mon

This script is in it's late beta stages, a memory leak monitor for HP-UX, Solaris and Linux.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
itai weisman
Super Advisor

Re: getopt

ok,
if I have two switches that are conflicts each other (like -l and -L on lvcreate command...) how can I check it with getopts?
James R. Ferguson
Acclaimed Contributor
Solution

Re: getopt

Hi:

> if I have two switches that are conflicts each other (like -l and -L on lvcreate command...) how can I check it with getopts?

Notice in the example given that as each switch is seen that a variable is set denoting that. Hence, after 'getopt' runs, write tests like:

if [ "$l_flag" = 1 -a "$L_flag" = 1 ]; then
print -u2 "Error: '-l' and '-L' are mutually exclusive"
fi

...

Since errors should be directed to STDERR I used 'print -u2' to target that file descriptor (FD2).

Regards!

...JRF...
Peter Nikitka
Honored Contributor

Re: getopt

Hi,

in addition to SEP and JRF:
If you have to deal with plain parameters as well, don't forget to reset the argument list.

while getopts :al:L:n op
do
case $op in
a|n) ${op}flag=y ;;
l) ${op}flag=$OPTIND
if [ "$Lflag" ]; then print -u2 'mutually exclusive option -l|-L ...'; exit 2;;
L) ${op}flag=$OPTIND
if [ "$lflag" ]; then print -u2 'mutually exclusive option -l|-L ...'; exit 2;;
?) print -u usage ;;
esac
done
# reset arglist
shift $((OPTIND-1))

typeset -i p=0
for i # all remaining parameters
do
print param$((p+=1))="'$i'"
done


mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Dennis Handly
Acclaimed Contributor

Re: getopt

There is info under ksh(1) and sh-posix(1). It also has its own man page, getopt(1) and getopts(1), getopt(3):
http://docs.hp.com/en/B2355-60130/getopts.1.html
http://docs.hp.com/en/B2355-60130/getopt.1.html
http://docs.hp.com/en/B2355-60130/getopt.3C.html
itai weisman
Super Advisor

Re: getopt

can I use getopts with switches that has more than one character? (like -itai)
Dennis Handly
Acclaimed Contributor

Re: getopt

>can I use getopts with switches that has more than one character? (like -itai)

Not directly. You would have to have -i with a parm that is "tai". Or you would have to leave out -i and process it as an illegal option.
James R. Ferguson
Acclaimed Contributor

Re: getopt

Hi (again) Itai:

> can I use getopts with switches that has more than one character? (like -itai)

No, 'getopt' and 'getopts' are single-character oriented. Perl offers a 'Getopt::Long' module where options are introduced with double dashes and can have long names, like '--help' or '--verbose'.

Regards!

...JRF...
dirk dierickx
Honored Contributor

Re: getopt

read this excellent guide on shelldorado for some more info.

http://www.shelldorado.com/goodcoding/cmdargs.html
Peter Nikitka
Honored Contributor

Re: getopt

Hi,

in (another) addition to JRF regarding the usage of multi-character-options:
If you supply somthing like '-ita' AND NONE of the options is defined to get a parameter, all of the options i,t,a are set.
Try with my (corrected) testscript:

#!/usr/bin/ksh
while getopts :al:L:n op
do
case $op in
a|n) eval ${op}flag=y ;;
l) eval ${op}flag=$OPTARG
if [ "$Lflag" ]; then print -u2 'mutually exclusive option -l|-L ...'; exit 2;fi ;;
L) eval ${op}flag=$OPTARG
if [ "$lflag" ]; then print -u2 'mutually exclusive option -l|-L ...'; exit 2;fi ;;
?) print -u usage ;;
esac
done
# reset arglist
shift $((OPTIND-1))

typeset -i p=0
for i # all remaining parameters
do
print param$((p+=1))="'$i'"
done
print Flags:
set | fgrep flag=

Example:
opt.ksh -anL val
Flags:
Lflag=val
aflag=y
nflag=y

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"