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"
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
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.