A sample use:
#!/bin/sh
setopt() {
if [ -n "$1" -a -n "$2" ]; then
optname=opt_${1#--}
optval="\"$2\""
eval $optname="$optval"
shift
shift
setopt "$@"
fi
}
eval setopt $(getopt -a -l city:,country: -o "" -- "$@")
echo "City is ${opt_city}"
echo "Country is ${opt_country}"
You could use the same technique without getopt, but getopt has the added benefit of normalizing names and recognizing abbreviations (at least GNU getopt does).
$ ./opttest -city "New Delhi" -country India
City is New Delhi
Country is India
$ ./opttest -ci "New Delhi" -co India
City is New Delhi
Country is India
getoptavailable as a command on your system?