ksh options help

From: Dave Markham (dave.markham@fjserv.net)
Date: Tue Jan 30 2007 - 07:40:24 EST


People im trying to do a script which needs options passed to it.

The problem i have is if an option which requires an argument is passed
one of the other options i need it to bum out.

Below is what i have got but am not sure if theres anything easier. I've
looked at getopts also it doesnt seem to work correctly.

Basically in the code below if the script is called with -function
-server i need it to bum out as it should be -function <function>
-server <server>. I have included the usage so you know how it should work

# [ Usage ]
usage()
{
        echo "\n\tUsage: $0 [-help] [-V]\n\t[-config_file <config file
location>]
        [-function <concord|openview>] [-remote_server [<servername>|<IP
address>]]\n"
}

# Get the options
while [ $# -ge 1 ];do
case "$1" in
-help|--help)
        help #Call help function
        exit 0
        ;;
-V)
        # VERSION = Version of this script
        VERSION=$(awk '/^#\ [0-9]/ {print $2}' $0 | tail -1)
        echo "Remote Site Backup Program: ${0#*./} Version [$VERSION]"
        exit 0
        ;;
-function)
        if [ "$2" != "concord" ] && [ "$2" != "openview" ];then
                usage
                exit 1
        fi
        shift
        function=$1
        ;;
-config_file)
        ## Below is getting messy. Look at getopts ##
        if [ ! "$2" ] || [ "$2" == "-remote_server" ] || [ "$2" ==
"-function" ]\
        || [ "$2" == "-V" ] || [ "$2" == "-help" ] || [ "$2" ==
"--help" ];then
                usage
                exit 1
        fi
        shift
        config_file=$1
        ;;
-remote_server)
        if [ ! "$2" ] || [ "$2" == "-config_file" ] || [ "$2" ==
"-function" ]\
        || [ "$2" == "-V" ] || [ "$2" == "-help" ] || [ "$2" ==
"--help" ];then
        shift
        remote_server=$1
        ;;
*)
        usage
        ;;
esac
shift
done
_______________________________________________
sunmanagers mailing list
sunmanagers@sunmanagers.org
http://www.sunmanagers.org/mailman/listinfo/sunmanagers



This archive was generated by hypermail 2.1.7 : Wed Apr 09 2008 - 23:41:33 EDT