Date: Mon, 12 May 1997 10:33:14 +0200 (MET DST) From: Zahemszky Gabor <zgabor@CoDe.hu> To: freebsd-questions@freebsd.org (FreeBSD questions) Cc: un_x@anchorage.net Subject: Re: getopts Message-ID: <199705120833.KAA00648@CoDe.hu> In-Reply-To: <Pine.BSF.3.95q.970511082828.18804A-100000@aak.anchorage.net> from Steve Howe at "May 11, 97 08:51:46 am"
next in thread | previous in thread | raw e-mail | index | archive | help
>
> ok - i finally figured this out, and am posting my answer
> for anyone else that may be interested ... there are a few
> tricky aspects of it (w/thanks to zgabor).
> -------------------------------------------------------------------------
> while getopts abc i; do
> case $i in
> a) a=$i; echo A;;
> b) b=$i; echo B;;
> c) c=$i; echo C;;
> ?) e=$i; echo huh?;;
> esac
> done
> shift $(($OPTIND - 1))
> echo $a + $b + $c
> echo $0 $1
Hm.
?) e=$OPTARG ; echo $OPTARG 'is an illegal option or an option
without a parameter' ;;
And it would be better :
while getopts :abc i ; do
--------------^
In that way, you suppress the getopts' own error message, and get back
a '?' if you give an illegal option, and get back a ':' if you give a legal
option which needs a parameter, but it's missing, eg:
Call the script with -x -a -b -c, but the -x is illegal, and it's missing
the -c's parameter.
while getopts :abc: i ; do
case "$i" in
a) echo option A ;;
b) echo option B ;;
c) echo option C with argumentum "$OPTARG" ;;
\?) echo illegal option "$OPTARG" ;;
:) echo "$OPTARG" option requires an argument ;;
?) echo 'What an earth is it? Getopts cannot return
with anything else' ;;
esac
done
Well, on 2.1.5, sh's getopts cannot handle this, I don't know the newer one.
(But it's the way, getopts works in other shells. Get pdksh, and man it.
It has a very good description about getopts - and about any other shell
construct.)
> -------------------------------------------------------------------------
> this script may be invoked as
> "script -abc file" or
> "script -bc -a file" or
> "script -c -cc file" ... (C will echo 3x)
>
> the "shift" statement MUST occur
> as above, or any "file" will not
> appear in "$1" when option parsing
> is done. you CANNOT replace the
> "shift" statement above with
>
> "a) a=$i; echo A; shift;;"
>
> type case statements, otherwise
>
> "script -abc file"
>
> where "-abc" is "$1"
> will get shifted 3x, and
> you will lose "file".
>
> any number of options in the getopts statement
> may be followed by a colon, in which case,
> $OPTARG will contain the options argument,
> which should be "caught" by its corresponding
> options "case" statement.
>
> the "e" case statement will trap any errors.
>
> i don't know if "getopts" can be used
> without a "while" statement ...
Yes it can, but it's not too effective ;-).
Gabor
--
#!/bin/ksh
Z='21N16I25C25E30, 40M30E33E25T15U!' ;IFS=' ABCDEFGHIJKLMNOPQRSTUVWXYZ ';set $Z;for i { [[ $i = ? ]]&&print $i&&break;[[ $i = ??? ]]&&j=$i&&i=${i%?};typeset -i40 i=8#$i;print -n ${i#???};[[ "$j" = ??? ]]&&print -n "${j#??} "&&j=;typeset +i i;};IFS=' 0123456789 ';set $Z;X=;for i { [[ $i = , ]]&&i=2;[[ $i = ?? ]]||typeset -l i;X="$X $i";typeset +l i;};print "$X"
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199705120833.KAA00648>
