linux-users archive

Re: getopts


New Message Reply Date view Thread view Subject view Author view

From: John Wiersba (email_suppressed_at_lugwash.org)
Date: Tue 26-May-2009 10:32:32 PM EDT


Here is a program template that I use:

usage() {
   rc=0 && (( $# > 0 )) && rc=1 && exec >&2 && echo "${0##*/}: $*"
   /bin/cat <<EOF
usage: ${0##*/} [-a ARG] [-b] FILE...
  -a ARG blah blah
  -b blah blah
EOF
   exit $rc
}
    
opt_a=
val_a=
opt_b=
while getopts :a:b opt ;do
   case $opt in
      a) opt_a=SPECIFIED ; val_a=$OPTARG ;;
      b) opt_b=SPECIFIED ;;
      :) usage "option $OPTARG requires an argument" ;;
      *) [[ $OPTARG = \? ]] && usage ; usage "unrecognized option $OPTARG" ;;
   esac
done
shift $((OPTIND-1))
[[ $# = 0 ]] && usage "at least one FILE must be specified"
echo "$# args: $*"
echo "a=<$opt_a> ($val_a)"
echo "b=<$opt_b>"
exit 0

----- Original Message ----
From: Hans Kokx <[e-mail suppressed]>
To: Lugwash Mailing List <[e-mail suppressed]>
Sent: Tuesday, May 26, 2009 5:32:10 PM
Subject: [WLUG] getopts

Hey all,

Writing another script (actually, expanding on an earlier one) and I need some help with getopts.

I'd like the script to run, and do it's normal activities when no flags are given. However, when a flag is given, i'd like it to do something different, but ignore the normal subroutine entirely. This is in bash. Anyone have any pointers, literature, or anything?

Hans

--
***  Sent from [e-mail suppressed]  ***  http://www.lugwash.org
to unsubscribe: `echo "unsubscribe" | mail [e-mail suppressed]`
--
***  Sent from [e-mail suppressed]  ***  http://www.lugwash.org
to unsubscribe: `echo "unsubscribe" | mail [e-mail suppressed]`

New Message Reply Date view Thread view Subject view Author view

This archive was generated by hypermail 2.1.5 : Mon 01-Jun-2009 01:00:02 AM EDT