AIX

AIX

Connect with fellow AIX users and experts to gain knowledge, share insights, and solve problems.


#Power
#Power
 View Only

Best Practices Q: framework/skeleton for CLI executable /bin/sh script

  • 1.  Best Practices Q: framework/skeleton for CLI executable /bin/sh script

    Posted Wed September 19, 2007 04:02 PM

    Originally posted by: SystemAdmin


    We are currently using

    set -- $(getopt ... $*)
    ...
    while test "--" != "$1"
    do
    case "$1" in
    ...
    esac
    shift
    done

    as described by getopt(1)

    This is good enough for most of our executables,
    but it has two significant limitations.

    1. It fails to properly handle quoted spaces in CLI options.
    2. It does not read from a CLI named configuration file.

    Does anyone have a sample (or pointer to sample) that
    will properly handle spaces in /bin/sh CLI option arguments?

    foo -a 'this has space'

    I have tried using $@,
    but so far without success.
    I think an example would provide the missing details.

    Does anyone have a sample (or pointer to sample) that
    will read arguments from a file argument to -f option,
    before processing the rest of the options?

    foo -f configfile -a 'override'
    foo -a 'override' -f configfile

    I have tried parsing once with getopts,
    scanning the arguments for a configuration file,
    reading the configuration file, and
    scanning the arguments as indicated by getopt(1)

    set -- $(getopts ... $*)
    for f in $*
    do
    case "$f" in
    ...
    -f) CONF=...
    ...
    esac
    done
    if test -r "$CONF"
    then
    read-conf-file
    fi
    while test "--" != "$1"
    do
    case "$1" in
    ...
    esac
    ...
    shift
    done

    The limitations of the above include
    1. option argument spaces are still mishandled
    2. reading the conf file is currently inelegant at best
    and ad hoc at worst

    Does anyone have a simple (or pointer to sample)
    that parses a config file parallel to CLI options?

    Ideally, I would like to
    1. read option and possible argument from each conf file line
    2. prepend the options and arguments to the argument list
    3. rerun getopt
    so that there would be no difference between whether an
    option was supplied on the command line or in the conf file.

    TIA,
    #AIX-Forum