I am trying to write a ksh script that takes an optional flag and two mandatory strings as argument. The flag is denoted as -a
. Thus the command look like one of the following when correct:
command.sh -a -b abc -c 123
command.sh -b xyz -c 789
I am using the following code in my script:
while getopts "a:b:c:" args
do
case $args in
a) # Flag
flag=1
;;
b) # str1
str1=$OPTARG
;;
c) # str2
str2=$OPTARG
;;
*) # usage
echo "- - - - "
exit 0
;;
esac
done
if [[ -z $str1 || -z $str2 ]]
then
echo "Incomplete arguments supplied\n"
exit 1
fi
...
Doing so when I execute 1 (see above) it throws me the message Incomplete arguments supplied
where as 2 (see above) is working fine.
Can anyone point out what is going wrong and recommend a rectification?
Thanks...