From owner-freebsd-questions Sat May 13 1:28: 1 2000 Delivered-To: freebsd-questions@freebsd.org Received: from rhenium.btinternet.com (rhenium.btinternet.com [194.73.73.93]) by hub.freebsd.org (Postfix) with ESMTP id 886FD37B799 for ; Sat, 13 May 2000 01:27:55 -0700 (PDT) (envelope-from mark@ukug.uk.freebsd.org) Received: from [213.1.104.25] (helo=parish.my.domain) by protactinium with esmtp (Exim 2.05 #1) id 12qKZI-00076t-00; Fri, 12 May 2000 19:53:48 +0100 Received: (from mark@localhost) by parish.my.domain (8.9.3/8.9.3) id TAA02159; Fri, 12 May 2000 19:52:33 +0100 (BST) (envelope-from mark) Date: Fri, 12 May 2000 19:52:33 +0100 From: Mark Ovens To: Erik Trulsson Cc: questions@freebsd.org Subject: SOLVED Re: getopt(1) or getopts(1)? Message-ID: <20000512195233.F233@parish> References: <20000511231319.C1522@parish> <20000512084656.A1146@student.csd.uu.se> <20000512183403.A233@parish> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0.1i In-Reply-To: <20000512183403.A233@parish>; from mark@ukug.uk.freebsd.org on Fri, May 12, 2000 at 06:34:03PM +0100 Organization: Total lack of Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG On Fri, May 12, 2000 at 06:34:03PM +0100, Mark Ovens wrote: > On Fri, May 12, 2000 at 08:46:56AM +0200, Erik Trulsson wrote: > > On Thu, May 11, 2000 at 11:13:19PM +0100, Mark Ovens wrote: > > > Can someone clarify getopt(1) and getopts(1)? According to sh(1): > > > > > > getopts optstring var > > > The POSIX getopts command. The getopts command deprecates the > > > older getopt(1) command..... > > > > > > but there is no manpage for getopts(1), only getopt(1). The latter > > > includes some sample code which works fine, however if I change > > > ``getopt'' to ``getopts'' in this code I get: > > > > > > parish:/usr/marko{89}% ./foobar -b > > > getopts: -b: bad variable name > > > Usage: ... > > > parish:/usr/marko{90}% > > > > > > Since getopt(1) is deprecated it would be better to use getopts(1). > > > Can anyone explain the above error, or point me to some documentation > > > for getopts(1)? > > > > > > > On my system (4.0-stable) there is a manpage for getopts(1). It just a link > > to buiiltin(1) which says that it is a builtin command in sh(1). > > Same here (I'm also on 4-stable). I hadn't spotted that it is a copy > of (not a link to) builtin(1). > > > The manpage for sh(1) has the following to say about getopts: > > > > getopts optstring var > > The POSIX getopts command. The getopts command deprecates the > > older getopt(1) command. The first argument should be a series > > of letters, each possibly followed by a colon which indicates > > that the option takes an argument. The specified variable is set > > to the parsed option. The index of the next argument is placed > > into the shell variable OPTIND. If an option takes an argument, > > it is placed into the shell variable OPTARG. If an invalid option > > is encountered, var is set to `?''. It returns a false value (1) > > when it encounters the end of the options. > > > > > > The first couple of lines of which I quoted in my original post so, > yes, I have read it. However it reads as though the syntax is the same > as getopt(1) (at least to me it does). So the question remains; why > does the sample code in getopt(1) not work if I change ``getopt'' to > ``getopts'' in the first line? > > I'm quite happy to RTFM, if only I could find a FM to R :) > I've got it now :) Re-reading sh(1) several times made me realize that "var" is not the input args (-a -b -c) as I thought, but a variable to store the output from getopts(1) and that it returns an errno, not the parsed string. I guess that's what you were pointing out to me (it's been a long day). Trying it without args resulted in a useful Usage: message getopts: Usage: getopts optstring var [arg] So I have to use it in a loop thus: while getopts abo: arg $* do .... done Thanks for your help. > > > > > > > > > > To Unsubscribe: send mail to majordomo@FreeBSD.org > > with "unsubscribe freebsd-questions" in the body of the message > > -- > ...and on the eighth day God created UNIX > ________________________________________________________________ > FreeBSD - The Power To Serve http://www.freebsd.org > My Webpage http://ukug.uk.freebsd.org/~mark/ > mailto:mark@ukug.uk.freebsd.org http://www.radan.com > -- ...and on the eighth day God created UNIX ________________________________________________________________ FreeBSD - The Power To Serve http://www.freebsd.org My Webpage http://ukug.uk.freebsd.org/~mark/ mailto:mark@ukug.uk.freebsd.org http://www.radan.com To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message