Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 7 May 1997 16:40: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:  <199705071440.QAA00712@CoDe.hu>
In-Reply-To: <Pine.BSF.3.95q.970506221747.18627B-100000@aak.anchorage.net> from Steve Howe at "May 6, 97 10:20:35 pm"

next in thread | previous in thread | raw e-mail | index | archive | help
> 
> 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"



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199705071440.QAA00712>