AST users,

I'd appreciate your help with this. This message is long, but, hopefully, it's clear. In the following script, "vas", I want getopts to consider the arguments that might accompany options "u" and "s" to be "required" arguments. However, it doesn't.

#! /bin/ksh
USAGE="[+NAME?${0##*/}]
      [+DESCRIPTION?Lookup a user.]
      [u:username?The user's username.]:[user]
      [s:surname?The user's last name.]:[last]"

while getopts "$USAGE" opt
do
  case $opt in
     u) un="$OPTARG" ;;
     s) sn="$OPTARG" ;;
    \?) print -u2 "The \"?\" case. OPTARG: \"$OPTARG\"."; exit -1 ;;
     :) print -u2 "The \":\" case. OPTARG: \"$OPTARG\"."; exit -1 ;;
  esac
done

shift OPTIND-1
print "un: \"$un\", sn: \"$sn\", $...@} remaining args: $*"
#-------------------------


EXAMPLE executions that seem OK:

$ ./vas -?
Usage: ./vas [-u user] [-s last]

$ ./vas --??long
Usage: ./vas [--username=user] [--surname=last]

$ ./vas -u jdoe -s Doe "Hello world" x
un: "jdoe", sn: "Doe", 2 remaining args: Hello world x

$ ./vas -u jdoe -s
./vas: -s: last argument expected
The "?" case. OPTARG: "".


EXAMPLE executions that don't give what I expected:

$ ./vas -u -s Doe "Hello world" x
un: "-s", sn: "", 3 remaining args: Doe Hello world x

$ ./vas -u jdoe -s -- "Hello world" x
un: "jdoe", sn: "--", 2 remaining args: Hello world x


ALSO, if I add a colon character as the first character value of getopt's "optstring" argument, then (for example) the following doesn't give the expected usage message:

$ ./vas --??
The "?" case. OPTARG: "?".

I see the above behavior on my Mac and on a CentOS Linux machine. The following gives OS and ksh version information on those hosts:

On my Mac:

$ uname -a
Darwin Dans-Computer.local 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:57:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_PPC Power Macintosh

$ print ${.sh.version}
Version M 1993-12-28 s+

On the Linux box:

$ uname -a
Linux xxxxx 2.6.18-128.7.1.el5 #1 SMP Mon Aug 24 08:21:56 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux

$ cat /etc/issue
CentOS release 5.3 (Final)
Kernel \r on an \m

$ print ${.sh.version}
Version M 93s+ 2008-01-31

BTW -- The reported date on the above line is wrong; right?  ;-)

Thanks,
Dan

_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users

Reply via email to