1832901 Members
2816 Online
110048 Solutions
New Discussion

Script Help

 
Waqar Razi
Regular Advisor

Script Help

Can some one help me understand the following script?

#-----------------
# functions
#-----------------
function Usage {
echo "\nUsage: $1 [ -h ] \n"
echo "where"
echo " -h = prints this message"
echo "example: $1 \n "
}
#-----------------

# Collect the input variables. Check to see if they are correct.

while getopts ":h" PARAMETER; do
case $PARAMETER in
h) # Need some help.
Usage $0
exit 1
;;
\?)
Usage $0
exit 1
;;
esac
done

# Cleanout the variable list
shift $(($OPTIND - 1))

This is in the beginning of the vg, lv creation scripts? Can some one please help me understand what this part of script does?
3 REPLIES 3
Dennis Handly
Acclaimed Contributor

Re: Script Help

This script calls getopts(1) to crack the input line. It is looking for "-h" and invoking the help message in the function Usage.
Unrecognized options goto the "?" case.

After that is done, it shifts off the processed args, for OPTIND -1 args.
James R. Ferguson
Acclaimed Contributor

Re: Script Help

Hi:

This is a standard way of processing commandline switches and arguments. In this case, only one optional switch ('-h') is allowed. Specifying '-h', or for that matter, any other flag or switch (an letter beginning with a hyphen) prints the name of the script and a usage message and then exits.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Script Help

Hi (again):

By the way, in case it wasn't clear, Dennis pointed you to the manpages for 'getopts' and those for 'sh-posix':

http://docs.hp.com/en/B2355-60130/getopts.1.html

http://docs.hp.com/en/B2355-60130/sh-posix.1.html

On the shell manpage, see also the information about OPTARG, OPTERR and OPTIND.

Regards!

...JRF...