Parameter parsing

Hi. I need to parse a string with several optional parameters such as the following:

PARAMS="-server testserver -type standby -host testhost1"

How can I parse this to place any values after the -parameter into a new variable? E.g I want to place testserver into a variable called SERVER. But importantly, sometimes the parameters may have no values and sometimes the actual -param itself may not be in the string and also there will be an optional (preceding) DEBUG value.

I've tried a while loop with a case statement but it doesn't handle the situation when the -param is not included.

arg="DEBUG -dbversion 11.2.0.4.0 -type standby -host "
 typeset flags_args=''
for arg; do
if [[ $arg == -* ]]; then
    case $arg in
        -dbversion )  flags_args="${flags_args} -d" ;;
        -type )  flags_args="${flags_args} -t" ;;
        -host ) flags_args="${flags_args} -h" ;;
        * )       flags_args="${flags_args} $arg" ;;
    esac
fi
done
# push the args back on input stack
set -- ${flags_args}
print "New Parameters: $*"
 
# now use getops
while getopts :d:t:h: args; do
    case $args in
        d) typeset _VER="$OPTARG" ; [[ -z ${_VER} ]] && _VER=NONE ;;
        t) typeset _TYPE="$OPTARG" ; [[ -z ${_TYPE} ]] && _TYPE=NONE ;;
        h) typeset _HOST="$OPTARG" ; [[ -z ${_HOST} ]] && _HOST=NONE ;;
        *) usage ;;
    esac
done
shift $((OPTIND-1));
print "VER: $_VER"
print "TYPE: $_TYPE"
print "HOST: $_HOST"

Any help greatly appreciated.

Hi.

Some possibilities to consider:

Process command-line (CLI) options, arguments

        1) getopts, builtin, bash, ksh, zsh
           http://stackoverflow.com/questions/402377/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options

        2) perl: libgetopt-euclid-perl, creates man page, help automatically

        3) getopt, enhanced getopts, part of the util-linux, allows GNU "--"
           Examples: /usr/share/doc/util-linux/examples, 2016.03.27

        4) argp.sh, wrapper for getopt, creates man and help (text, XML), etc.
           Allows mixed options and arguments.
           Compiled argp.c -> argp for faster execution.
           https://sourceforge.net/projects/argpsh/, 2016.03.27

        5) shflags, wrapper for getopt, creates help, allow mixed options
           and arguments
           https://github.com/kward/shflags, 2016.08.01

        6) ksh getopts, enhanced, process GNU "--", creates man, help, etc.
           Examples: Learning the Korn Shell, O'Reilly, 2nd, p 380ff

        7) zsh zparseopts
           man zshmodules, part of zshutil

Best wishes ... cheers, drl

With a recent shell offering "here strings" and "arithmtic expansion", you could try

arg="DEBUG -dbversion 11.2.0.4.0 -type standby -host "
ARR=($arg)
for ((i=0; i<${#ARR[@]}; i++)); do TMP=${ARR[$i]}; TMPM=${ARR[$i]/-}; [ $TMP = $TMPM ] && read ${TMP^^} <<< 1 || read ${TMPM^^} <<< ${ARR[((++i))]}; done
echo $DEBUG, $DBVERSION, $TYPE, $HOST
1, 11.2.0.4.0, standby,

Thanks Rudi. Getting the following when I run it from within a script (although it works on the cmdln):

./p.ksh: line 4: syntax error at line 5: `^' unexpected

Unfortunately, in my proposal there's neither a line 4 not a line 5. PLEASE get accustomed to post a decent context!
I guess ksh doesn't like the "Case Modification" Parameter Expansion known from bash . Try to typeset -u TMP TMPM and remove the ^^ in the code line.

1 Like

Works perfectly. Thank you very much.