From owner-freebsd-questions Sun May 11 10:01:32 1997 Return-Path: Received: (from root@localhost) by hub.freebsd.org (8.8.5/8.8.5) id KAA04639 for questions-outgoing; Sun, 11 May 1997 10:01:32 -0700 (PDT) Received: from iceberg.anchorage.net. (root@iceberg.anchorage.net [207.14.72.150]) by hub.freebsd.org (8.8.5/8.8.5) with SMTP id KAA04627 for ; Sun, 11 May 1997 10:01:28 -0700 (PDT) Received: from aak.anchorage.net (ai-130 [207.14.72.130]) by iceberg.anchorage.net. (8.6.11/8.7.3) with SMTP id HAA30712 for ; Sun, 11 May 1997 07:59:11 -0800 Date: Sun, 11 May 1997 08:51:46 -0800 (AKDT) From: Steve Howe X-Sender: abc@aak.anchorage.net To: freebsd-questions Subject: getopts Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-questions@freebsd.org X-Loop: FreeBSD.org Precedence: bulk ok - i finally figured this out, and am posting my answer for anyone else that may be interested ... there are a few tricky aspects of it (w/thanks to zgabor). ------------------------------------------------------------------------- while getopts abc i; do case $i in a) a=$i; echo A;; b) b=$i; echo B;; c) c=$i; echo C;; ?) e=$i; echo huh?;; esac done shift $(($OPTIND - 1)) echo $a + $b + $c echo $0 $1 ------------------------------------------------------------------------- this script may be invoked as "script -abc file" or "script -bc -a file" or "script -c -cc file" ... (C will echo 3x) the "shift" statement MUST occur as above, or any "file" will not appear in "$1" when option parsing is done. you CANNOT replace the "shift" statement above with "a) a=$i; echo A; shift;;" type case statements, otherwise "script -abc file" where "-abc" is "$1" will get shifted 3x, and you will lose "file". any number of options in the getopts statement may be followed by a colon, in which case, $OPTARG will contain the options argument, which should be "caught" by its corresponding options "case" statement. the "e" case statement will trap any errors. i don't know if "getopts" can be used without a "while" statement ...