From: Dave Markham (dave.markham@fjserv.net)
Date: Tue Jan 30 2007 - 08:13:36 EST
People i did try getopts like below, but it doesnt seem to 1. use words,
and 2 usage isnt printed if the OPTARG isnt found or whats to stop -f
then -c being put on the line instead of -f <function> then -c ?
### Tried getopts but doesnt seem to handle if dont give an arg to an option
#while getopts ":c:f:s:hV" opt ; do
#case $opt in
#h)
#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
#;;
#f)
#if [ "$OPTARG" != "concord" ] && [ "$OPTARG" != "openview" ];then
#usage
#exit 1
#fi
#function=$OPTARG
#;;
#c)
#if [ ! "$OPTARG" ];then
#usage
#exit 1
#fi
#config_file=$OPTARG
#;;
#s)
#if [ ! "$OPTARG" ];then
#usage
#exit 1
#fi
#remote_server=$OPTARG
#;;
#\?)
#usage
#;;
#esac
#done
#shift $(($OPTIND - 1))
Dave Markham wrote:
> 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
_______________________________________________
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