function Parse
{
while getopts :r:l:p:o:q:t: name
do
case $name in
l) LOG="$OPTARG" ;; # LOG FILE
p) PROC="$OPTARG" ;; # Process name
o) ONE_UP="$OPTARG" ;; # One_Up Number
q) PRNT="$OPTARG" ;; # q Print queue 'FTP' for now
r) LIS="$OPTARG" ;; # lis file name
t) FTPDEST="$OPTARG" ;; # D = Disk, T = Tape
*) Usage ;; # display usage and exit
esac
done
Hi Jim mcnamara and friends,
I used getopts in the way you suggested. But what if i have to send two mandatory arguments to the script and the rest two as optional parameters ?
i have having problem in giving both Mandatory and optional fields at the same time to the script.
Please help.
Thanks in advance
Veera
In Jim's example, he's setting variables; therefore, he could test the variables with [ -z "${VAR...}" ] and create appropriate exceptions if they aren't defined.
function Parse
{
while getopts :r:l:p:o:q:t: name
do
case $name in
l) LOG="$OPTARG" ;; # LOG FILE
p) PROC="$OPTARG" ;; # Process name
o) ONE_UP="$OPTARG" ;; # One_Up Number
q) PRNT="$OPTARG" ;; # q Print queue 'FTP' for now
r) LIS="$OPTARG" ;; # lis file name
t) FTPDEST="$OPTARG" ;; # D = Disk, T = Tape
*) Usage ;; # display usage and exit
esac
done
[ -z "${PROC}" -o -z "${PRNT}" ] && Usage
You can take this a step further by using a check sum. Say you have 5 or 10 required flags, you could accomplish this by keeping track of those arguments you have already processed by using a counter. Check the value of the counter at the end to verify all flags were received.
USAGE="Usage: `basename $0` [-s schemaname] [-d databasename] [-u username] -f -t -h"
while getopts :s:d:u:fth params
do
case $params in
s) SOURCESCHEMA="$OPTARG"
let COUNT= $COUNT+10000
;;
f) FULL='y'
;;
t) TABLE='y'
;;
d) DBNAME="$OPTARG"
let COUNT= $COUNT+1
;;
u) USERNAME="$OPTARG"
let COUNT= $COUNT+1000
;;
h)
help_doc
exit 0
;;
?)
echo "Invalid Option Specified"
echo "$USAGE" 1>&2 ; exit 1
;;
esac
if [ "$COUNT" -ne 10101 ]
then
help_doc
exit 0
elif [ ! -z $FULL ]
then
echo "Paramater FULL is Set"
elif [ ! -z $TABLE ]
then
echo "Paramater TABLE is Set"
else
echo "Neither TABLE or FULL Are Set"
fi