Hi Mark, You and me both ;^)
Until recently, I thought that getopt(1) was POSIX, whereas it is in fact getopts(1), and it is not a shell built-in there, but a utility[0]. [0] https://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html Cheers, Raf On Fri, Apr 30, 2021 at 10:54:16AM BST, Marc Espie wrote: > Until a patch from naddy, I wasn't even aware of getopts in sh(1) > > Unless I made some mistakes, this translates the example in getopt(1) > manpage. > > It's likely some stronger wording might be adequate, I suspect some > of the BUGS section in getopt(1) does not apply to the sh(1) built-in. > > Index: bin/ksh/sh.1 > =================================================================== > RCS file: /cvs/src/bin/ksh/sh.1,v > retrieving revision 1.152 > diff -u -p -r1.152 sh.1 > --- bin/ksh/sh.1 22 May 2019 15:23:23 -0000 1.152 > +++ bin/ksh/sh.1 30 Apr 2021 09:51:42 -0000 > @@ -508,6 +508,26 @@ is a colon, > .Ev OPTARG > is set to the unsupported option, > otherwise an error message is displayed. > +.Pp > +The following example has identical functionality to the > +example in > +.Xr getopt 1 . > +.Bd -literal -offset indent > +while getopts abo: name > +do > + case "$name" > + in > + a|b) > + flag="-$name";; > + o) > + oarg="$OPTARG";; > + ?) > + echo "Usage: ..." > + exit 2 > + ;; > + esac > +done > +.Ed > .It Ic hash Op Fl r | Ar utility > Add > .Ar utility > Index: usr.bin/getopt/getopt.1 > =================================================================== > RCS file: /cvs/src/usr.bin/getopt/getopt.1,v > retrieving revision 1.19 > diff -u -p -r1.19 getopt.1 > --- usr.bin/getopt/getopt.1 16 Mar 2018 16:58:26 -0000 1.19 > +++ usr.bin/getopt/getopt.1 30 Apr 2021 09:51:42 -0000 > @@ -54,7 +54,7 @@ which requires an argument. > args=`getopt abo: $*` > if [ $? -ne 0 ] > then > - echo 'Usage: ...' > + echo "Usage: ..." > exit 2 > fi > set -- $args > @@ -79,6 +79,11 @@ cmd -a -o arg file file > cmd -oarg -a file file > cmd -a -oarg -- file file > .Ed > +Note that > +.Xr sh 1 > +offers the > +.Ic getopts > +built-in with a simpler usage. > .Sh DIAGNOSTICS > .Nm > prints an error message on the standard error output when it >