Re: ksh options help



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@xxxxxxxxxxxxxxx
http://www.sunmanagers.org/mailman/listinfo/sunmanagers
_______________________________________________
sunmanagers mailing list
sunmanagers@xxxxxxxxxxxxxxx
http://www.sunmanagers.org/mailman/listinfo/sunmanagers



Relevant Pages

  • Re: getopts AGAIN!!!
    ... function usage { ... while getopts:x:y opt ... The way I've come to use getopts, is to set a flag for every switched ... exit -1;; ...
    (comp.unix.shell)
  • Re: Command line options: using an [option: parameter] without a parameter passed to it
    ... >> But if you test the results from getopts (which I always use to trigger a usage ... > as you care to specify. ... In other words, you told getopts that -f has an option-argument, so it better ...
    (comp.lang.perl.misc)
  • RE: I need help here
    ... Here's the Big Secret about Perl variables that most people learn ... use strict; ... : $outputfile = shift; ... You will often see a usage message in a script which deals ...
    (perl.beginners)
  • Re: will this script *always* work and pick out the file name?
    ... I write shell scripts for clinical trials programmers ... usage { ... shift $) ...
    (comp.unix.shell)
  • Re: QUESTION ON BIT SHIFTING !
    ... > Apart from usage in multiplication and division, ... one use is shift registers: power company can use it to have a sliding ... if a kwh is used during the shift period - a 1 is added to the left side - ... We used that scheme for a demand controller. ...
    (comp.lang.java.help)