Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getopts with long flags 1

Status
Not open for further replies.

meetramana

Programmer
Feb 2, 2005
57
US
is there any way to do the following using getopts in korn shell ?

myscript.ksh -input inputfilename -ouput outfilename

instead of

myscript.ksh -i inputfilename -o outfilename

where inputfilename and outfilename are the real filename and the rest are flags except the scriptname.

The above is just an example of what I am trying to do

I know it can be done through if and shift operators, but I like the getopts validation. SUN operating system accommodates something like with long flags such as

getopts "i:(input)" arguments
.....

but that requires to be invoked as
myscript.ksh --input inputfiles ....
not
myscript.ksh -input inputfile

 
Don't know if this helps:

Code:
#!/bin/ksh
# # # #
# canonicalize long options to flags
flags_args=''
for arg
do
 case $arg in
  -copy)
    flags_args="${flags_args} -c"
    ;;
  -send)
    flags_args="${flags_args} -s"
    ;;
  *)
    flags_args="${flags_args} $arg"
    ;;
 esac
done
# # #
# throw the canonicalized arg-string back
set -- ${flags_args}
# # #
# run through the options with getopts
cflag=0
sflag=0
while getopts c:s name
do
 case $name in
  s)
    sflag=1
    ;;
  c)
    cflag=1
    cpath="$OPTARG"
    ;;
  ?)
    printf "Usage: %s: [-send] [-copy backupfilename] args\n" $0
    printf "   or: %s: [-s] [-c backupfilename] args\n" $0
    exit 2
    ;;
 esac
done
# # #
# real work starts here
echo "$cflag $cpath"
echo "$sflag"


HTH,

p5wizard
 
Thankyou p5Wizard.

I did the conversion yesterday using sed.

But your code definitely helps to learn something new. I really liked it. Sweet...

Thanks again.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top