From owner-freebsd-questions Wed May 7 09:08:15 1997 Return-Path: Received: (from root@localhost) by hub.freebsd.org (8.8.5/8.8.5) id JAA29114 for questions-outgoing; Wed, 7 May 1997 09:08:15 -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 JAA29096 for ; Wed, 7 May 1997 09:08:09 -0700 (PDT) Received: by mail.EUnet.hu, id SAA16553; Wed, 7 May 1997 18:08:06 +0200 Received: (from zgabor@localhost) by CoDe.hu (8.7.5/8.7.3) id QAA00712; Wed, 7 May 1997 16:40:15 +0200 (MET DST) From: Zahemszky Gabor Message-Id: <199705071440.QAA00712@CoDe.hu> Subject: Re: getopts To: freebsd-questions@freebsd.org (FreeBSD questions) Date: Wed, 7 May 1997 16:40:14 +0200 (MET DST) Cc: un_x@anchorage.net In-Reply-To: from Steve Howe at "May 6, 97 10:20:35 pm" 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 > > can anyone tell me what the purpose of the sh builtin "getopts" > is? it doesn't seem to be as functional as the program > getopt(1). why would i want to use it (getopts)? > ------------------------------------------------------------------------- Because you cannot handle arguments with embedded spaces/tabs/newlines with getopt, but can it with getopts. Another one: it's a builtin utility, not an external, so a little bit more quick. Another one: POSIX. --- getopt --- set -- `getopt abc "$@"` while [ $1 != '--' ] ; do case "$1" in -a) echo A option ;; -b) echo B option ;; -c) echo C option ;; *) echo What is it? esac shift done echo $# "$@" --- getopts --- while getopts abc i ; do case "$i" in a) echo A option ;; b) echo B option ;; c) echo C option ;; *) echo What is it? ;; esac done shift $(( $OPTIND - 1 )) echo $# "$@" --- output --- Script started on Wed May 7 16:35:06 1997 # ksh lo1 -a -b -c -d 'a b c' getopt: illegal option -- d A option B option C option 4 -- a b c # ksh lo2 -a -b -c -d 'a b c' A option B option C option lo2: lo2[8]: -d: unknown option What is it? 1 a b c # Script done on Wed May 7 16:35:41 1997 ---- Bye, 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"