If I call my function with grouped options: "logm -TDIWEFO 'message' ", then only the "T" gets parsed correctly. The subsequent values returned have underscores prefixed to the value: "_D", "_I", etc. If I "logm -T -DIWEFO 'message' ", the "T" and the "D" are OK, but "I" through "O" get the underscores.
Can someone, please, point out what I'm doing wrong.
Here's the function (with debugging "echo"s included):
function logm
{
echo inside logm
#OPTSTRING=":tTdDiIwWeEfFoO"
#while getopts ${OPTSTRING} opt
while getopts ":tTdDiIwWeEfFoO" opt
do
echo OPTIND=${OPTIND}
echo OPTARG=${OPTARG}
echo opt=$opt
echo 1=$1
echo 2=$2
echo 3=$3
echo 4=$4
case ${opt} in
t|T)
echo log TRACE ${2}
log TRACE ${2}
;;
d)
echo log DEBUG ${2}
log DEBUG ${2}
;;
D)
echo log DEBUG ${2}
log DEBUG ${2}
;;
i|I)
echo log INFO ${2}
log INFO ${2}
;;
w|W)
echo log WARN ${2}
log WARN ${2}
;;
e|E)
echo log ERROR ${2}
log ERROR ${2}
;;
f|F)
echo log FATAL ${2}
log FATAL ${2}
;;
o|O)
echo log OFF ${2}
log OFF ${2}
;;
*)
echo unknown opt $opt
;;
esac
done
unset OPTSTRING
unset OPTIND
}
Here's how I call it:
logm -TDIWEFO "this is the tdiwefo message"
Here're the results:
inside logm
OPTIND=1
OPTARG=
opt=T
1=-TDIWEFO
2=this is the tdiwefo message
3=
4=
log TRACE this is the tdiwefo message
OPTIND=1
OPTARG=
opt=_D
1=-TDIWEFO
2=this is the tdiwefo message
3=
4=
unknown opt _D
OPTIND=1
OPTARG=
opt=_I
1=-TDIWEFO
2=this is the tdiwefo message
3=
4=
unknown opt _I
OPTIND=1
OPTARG=
opt=_W
1=-TDIWEFO
2=this is the tdiwefo message
3=
4=
unknown opt _W
OPTIND=1
OPTARG=
opt=_E
1=-TDIWEFO
2=this is the tdiwefo message
3=
4=
unknown opt _E
OPTIND=1
OPTARG=
opt=_F
1=-TDIWEFO
2=this is the tdiwefo message
3=
4=
unknown opt _F
OPTIND=2
OPTARG=
opt=_O
1=-TDIWEFO
2=this is the tdiwefo message
3=
4=
unknown opt _O