From owner-freebsd-questions Mon May 12 04:07:55 1997 Return-Path: Received: (from root@localhost) by hub.freebsd.org (8.8.5/8.8.5) id EAA12640 for questions-outgoing; Mon, 12 May 1997 04:07:55 -0700 (PDT) Received: from mail.EUnet.hu (www.eunet.hu [193.225.28.100]) by hub.freebsd.org (8.8.5/8.8.5) with ESMTP id EAA12618 for ; Mon, 12 May 1997 04:07:47 -0700 (PDT) Received: by mail.EUnet.hu, id NAA03001; Mon, 12 May 1997 13:07:35 +0200 Received: (from zgabor@localhost) by CoDe.hu (8.7.5/8.7.3) id KAA00648; Mon, 12 May 1997 10:33:15 +0200 (MET DST) From: Zahemszky Gabor Message-Id: <199705120833.KAA00648@CoDe.hu> Subject: Re: getopts To: freebsd-questions@freebsd.org (FreeBSD questions) Date: Mon, 12 May 1997 10:33:14 +0200 (MET DST) Cc: un_x@anchorage.net In-Reply-To: from Steve Howe at "May 11, 97 08:51:46 am" X-Mailer: ELM [version 2.4ME+ PL11 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-questions@freebsd.org X-Loop: FreeBSD.org Precedence: bulk > > 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"