Hi tronayne,
Thank you very much for the quick response. My apologies, looks like I have slightly misquoted the problem. The code you have written definitely retrieves the values that were otherwise lost.But still the problem of an option without a value eating the adjacent option remains.
Here my requirement is to capture the value along with the right option.In the new script I will not be sure what the value really means as it only displays the remaining arguments, after which I still don't know what they mean.
This is what I did with the new code:
Code:
#!/bin/ksh
# define a fatal error function
function fatal
{
print -u2 "${1}"
exit 1
}
# initialize flags to null
aflag=
bflag=
cflag=
dflag=
# define usage message
USAGE="Usage:\t${0} [-a value] [-b value] args"
# process command line arguments
while getopts :?a:b:c:d: name
do
case ${name} in
a) aflag=1
aval="${OPTARG}";;
b) bflag=1
bval="${OPTARG}";;
c) cflag=1
cval="${OPTARG}";;
d) dflag=1
dval="${OPTARG}";;
:) print -u2 "${0}:\t${OPTARG} requires a value"
fatal "${USAGE}";;
\?) print -u2 "${0}:\tunknown option"
fatal "${USAGE}";;
esac
done
if [ ! -z "${aflag}" ]
then
print "Option -a ${aval} specified"
fi
if [ ! -z "${bflag}" ]
then
print "Option -b ${bval} specified"
fi
if [ ! -z "${cflag}" ]
then
print "Option -c ${cval} specified"
fi
if [ ! -z "${dflag}" ]
then
print "Option -d ${dval} specified"
fi
shift $((${OPTIND}-1))
if [ "$*" ]
then
print "Remaining arguments are:\t${*}"
fi
exit 0
Test results of this script yielded the following:-
Code:
./testing -a -b bbb -d -c
Option -a -b specified
Remaining arguments are: bbb -d -c
What we see here is that -b gets assigned to "a" and the actual value of b (bbb) is detached from it. After this I will be left with a string of characters (bbb -d -c)which I will not be able to use intelligently.
Further elaborating, this script is triggered by a very rigid process written in java or c++ (I am not really sure) where it says:
Code:
/home/testing -a %access% -c %canceluser% -l %lenght% -s %setuser% -g %get% -t %trigger% -e %extratext% -u %user% -y %uptime% -z %create%
This is a fixed line from the program, now in reality this script will not always have values for all the variables placed within the % sign. This is when we come up with my problem. An example is like this
Code:
/home/testing -a 10:30 -c -l five -s smith -g get -t -e adding up things -u smith -y -z
What we see here is that a number of options like c,t,y,z do not have values. This kind of conditions should not affect the script, this script is a multi utility script which changes its behavior based on the available variable values. Hence I must some how be able to get the right values into the right variables.