Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot get getopts and case to assign values...

Status
Not open for further replies.

pmcmicha

Technical User
May 25, 2000
353
I am running the following command, but I can not getting a return value from getopts. I am on SCO Unixware 7.1.1 and running KSH 93.

#!/usr/bin/ksh -p

while getopts ":b:d:p:" ARG
do
case $ARG in
b)
COUNT=0
FCOUNT=2
;;
d)
COUNT=0
FCOUNT=1
;;
p)
COUNT=1
FCOUNT=2
;;
*)
USAGE # This is a function to inform users.
;;
esac
done

shift $(($OPTIND - 1))

OPT=$@


The script only takes one of the above options at a time.

Thanks in advance.
 
try this and let me know what you think.


#!/usr/bin/ksh -p

while getopts :b:d:p: ARG
do
case $ARG in
b)
COUNT=0
FCOUNT=2
;;
d)
COUNT=0
FCOUNT=1
;;
p)
COUNT=1
FCOUNT=2
;;
*)
USAGE # This is a function to inform users.
;;
esac
done



shift `let $OPTIND -1`

OPT=$@
 
thutmose,

I removed the double quotes and modified the shift statement to what you have here, but the script failed out with this error:

UX:ksh: ERROR: -: more tokens expected
UX:ksh: ERROR: :: syntax error

I then tried to modify the shift statement to this:
shift `let $OPTIND=$OPTIND-1` and got the message below:

UX:ksh: ERROR: 0=0-1: assignment requires lvalue
UX:ksh: ERROR: =0-1:: syntax error


Any other thoughts on this? Thanks.
 
Figured out the problem, should be:

#!/usr/bin/ksh -p

while getopts "bdp" ARG
do
case $ARG in
b)
COUNT=0
FCOUNT=2
;;
d)
COUNT=0
FCOUNT=1
;;
p)
COUNT=1
FCOUNT=2
;;
*)
USAGE # This is a function to inform users.
;;
esac
done

shift $(($OPTIND - 1))

OPT=$@


I just had to remove the colons from the while statement.
 
your problem wasn't anything to do with the colons and you don't need to quote the options. Your script falied because your shift expression was incorrect.

You had : shift $(($OPTIND -1))
should be: shift $((OPTIND -1))

Hope that helps!

Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top