This is my first question.
I'm trying to make a small script with options and I'm using getopts
to do it. The weird thing is:
The argument don't get assigned to the variable or it always enter the if
codeblock (that seems the problem).
Here is the script:
#!/bin/bash
function work () {
echo "It Works" }
function help () {
echo "Print some help and options" }
while getopts ha:b: OPT; do
case $OPT in
-h)
help
exit
;;
-a)
OPTA=$OPTARG
;;
-b)
OPTB=$OPTARG
;;
\?)
help
break
;;
esac done
echo $OPTB
if [[ -z $OPTB ]]; then
echo "ERROR. Needs OPTB"
fi
if [[ -n $OPTB ]]; then
echo "All OK untill here"
work
fi
It allways prints the ERROR message.