

getopts                      Command                      getopts




Parse command-line options

ggeettooppttss _o_p_t_s_t_r_i_n_g _n_a_m_e [ _o_p_t ]

The command  ggeettooppttss is available  under the Korn kksshh  to parse a
command's  options  and  check  their legality.   _o_p_t_s_t_r_i_n_g  must
contain the  options letters that the  command using ggeettooppttss will
recognize.  If  a letter is followed by a  colon `:', that option
must have an argument that is separated from it by whitespace.

Each time it is invoked,  ggeettooppttss places the next option into the
shell  variable _n_a_m_e  and the  index of the  next argument  to be
processed into the shell variable OOPPTTIINNDD, which is initialized by
default  to one.   When an option  requires an  argument, ggeettooppttss
copies it  into the shell variable  OOPPTTAARRGG. If ggeettooppttss encounters
an error, it initializes variable _n_a_m_e to ?.

When it  encounters the  end of  the options, ggeettooppttss  exits with
non-zero  status.   The  special option  ``--''  can  be used  to
delineate the end of options.

***** Example *****

The following  example processes a command  that takes options aa,
bb, and oo; the last option requires an argument:


        while getopts abo: c
        do
                case $c in
                        a|b)    FLAGS=$FLAGS$c;;
                        o)      OARG=$OPTARG;;
                        \?)     echo $USAGE 1>&2
                                exit 2;;
                esac
        done
        shift OPTIND-1


This code will accept any of the following as equivalent:


        cmd -a -b -o "xxx z yy" file
        cmd -a -b -o "xxx z yy" -- file
        cmd -ab -o "xxx z yy" file
        cmd -ab -o "xxx z yy" -- file


***** See Also *****

ccoommmmaannddss, ggeettoopptt(), kksshh





COHERENT Lexicon                                           Page 1


