Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

ksh: optional parameter ( possible use of getopts )

SOLVED
Go to Solution
Valued Contributor

ksh: optional parameter ( possible use of getopts )

hello,

 

i tried to use

getopts

 for following use. but i can't solve my task. i want to have a combination of optional parameters and also i want to know if optional parameter "-i"  is used.

 

  1. without parameters ( easy )
  2. only optional parameters ( easy )
  3. combination of optional parameters

example :

  1. script
  2. script -i -o     
  3. script -i -o param1 param1

 

the problem of "getopts"  is :

- using "-i" without parameters

- when i want to use more parameters i need  "

 

some inputs :

getopts manpage

ksh: using of getopts and checks of wrong inputs

 

 

#!/usr/bin/ksh

SCRIPT=${0##*/}

typeset use_option="FALSE"
typeset use_option_i="FALSE"
typeset use_option_help="FALSE"
typeset options=""

if [ $# -eq 0 ]
then
  :
else
  while [ $# -ge 1 ]
  do
    echo "\$1:$1 *:$*"
    case "${1}" in
     -h )         use_option_help="TRUE"
                  use_option="TRUE"
	          ;;
     -\? )        use_option_help="TRUE"
                  use_option="TRUE"
	          ;;
     -[a-zA-Z]* ) echo "${1}" | grep -q "\-i"
		  [ $? -eq 0 ] && use_option_i="TRUE"
		  if [ -z "${options}" ]
  		  then
  	            options="${1}"
                  else
  	            options="${options} ${1}"
                  fi
                  use_option="TRUE"
	          ;;
     *  )         params=$* ; 
	          shift $#
	          ;;
    esac
    if [ $# -gt 0 ]
    then
      shift
    fi
  done
fi

echo "use_option_i ${use_option_i}"
echo "options: ${options} params: ${params}"

exit 0

 

regards

 

11 REPLIES
Acclaimed Contributor Acclaimed Contributor

Re: ksh: optional parameter (possible use of getopts)

[ Edited ]

>but I can't solve my task. I want to have a combination of optional parameters and also I want to know if optional parameter "-i"  is used.

 

I don't see why getopts(1) can't be used.

 

>- using "-i" without parameters

>- when I want to use more parameters i need

 

These aren't parms to "-i".  These are just parms.

 

if [ $# -eq 0 ]; then

   :

 

Why not just test for "-ne 0", so you don't need this.

 

>echo "\$1:$1 *:$*"

 

You shouldn't be using $* if you want to handle embedded blanks.  Instead use "$@" and make sure in quotes.

 

 -h ) use_option_help="TRUE"

         use_option="TRUE"

         ;;

 -\? ) use_option_help="TRUE"

          use_option="TRUE"

        ;;

 

You should combine the two.  Also don't put a space before the ")":

  -h|-\?) use_option_help="TRUE"

                use_option="TRUE"  ;;

 

> -[a-zA-Z]* ) echo "${1}" | grep -q "\-i"

 

This "\" does nothing.  The proper syntax is:

 -[a-zA-Z]*) echo "${1}" | grep -q -e "-i"

 

Also, why check for "-i", just have a separate case for it.

 

> * ) params=$* ;

 

Here is where you want "$@":

  *) params="$@"

Valued Contributor

Re: ksh: optional parameter (possible use of getopts)

>> I don't see why getopts(1) can't be used.

 

when i want to use this combinations, how i handle this with "getopts" ?

 

    script -i -o     
    script -i -o param1 param1

the problem of "getopts"  is :

- when i want to use more parameters i need  "  like  script -i -o "param1 param2"


- getopts can only has "fixed" combinations , right ? 

   like : -o param1 param2

   but when i don't use  "-o " like -i param1 param2 it is possible to handle ?

   but how about

   -i -o param1 param2   or   -o -i param1 param2

  

 

here my code  for :  -i -o "param1 param2 " but not possible for  -i -o param1 param2 or when i use only param1 param2  (no opt. params ) then i have to use a code like above ?

typeset options=""

if [ $# -ne 0 ]
then
  while getopts hio: option
  do
    case "${option}" in
     i)   echo "Getopts_Option: i"
	  optarg_i="${OPTARG}" ;
	  ;;
     o)   echo "Getopts_Option: o"
	  optarg_o="${OPTARG}" ;
	  ;;
     h)   echo "Getopts_Option: h"
	  echo "usage $0 ...  "; exit 2
	  ;;
     [?]) echo "Getopts_Option: h"
	  echo "usage $0 ...  "; exit 2
	  ;;
    esac # case "${option}" in
  done

  # OPTIND    The index of the last option argument processed by the
  #           getopts built-in command.
  shift $(($OPTIND -1))

  if [ ${OPTIND} -le $# ]
  then
    echo "${0}: Non-option present at Position \"${OPTIND}\" : $( eval echo \$${OPTIND} )"
    echo "usage $0 ...  " ; exit 2
  fi
fi

 regards

Acclaimed Contributor Acclaimed Contributor

Re: ksh: optional parameter (possible use of getopts)

[ Edited ]

>I want to use more parameters I need, like  script -i -o "param1 param2"

 

You treat it as: script -i -o

Then you detect invalid options and then process them special.

 

I'm assuming there is NO connection between the -i and -o and param1 and param2??

Or is there?  What syntax to you want to allow and is there a connection between parms and options?

 

>- getopts can only has "fixed" combinations, right?

 

Yes.  It allows an optional "-(letter)" or "-(letter) parm" but not both with that same letter.

 

>when I don't use  "-o" like -i param1 param2 it is possible to handle?

 

Sure.

 

>-i -o param1 param2   or   -o -i param1 param2

 

Sure.

 

>here my code for:  -i -o "param1 param2" but not possible for  -i -o param1 param2 or when I use only param1 param2  (no opt. parms) then i have to use a code like above?

> while getopts hio: option

 

This getopts allows in any order and optional:

-h, -i or "-o o_parm"

 

(It also allows duplicates.)

 

The following in case "i" isn't valid: optarg_i="${OPTARG}"

Valued Contributor

Re: ksh: optional parameter (possible use of getopts)

hello,

 

>>I'm assuming there is NO connection between the -i and -o and param1 and param2??
>> Or is there? 

 

there is no connection,  "-i"  "-o"  can use in any order , but when i have to get the params "param1 param2" ,

i have to specify it in "getopts .... " , right ?

 

> What syntax to you want to allow and is there a connection between parms and options?

 

no connection,   "-i"  "-o"  are optional.

 

regards

Valued Contributor

Re: ksh: optional parameter (possible use of getopts)

hello,

 

here my  revised code of my first thread . I'm very ashamed of my bad program style ! thank you for your input's Dennis. Maybe you can teach me ( us ) with a web seminar ?

 

regards,

Acclaimed Contributor Acclaimed Contributor

Re: ksh: optional parameter (possible use of getopts)

>there is no connection,  "-i"  "-o"  can use in any order , but when i have to get the params "param1 param2" ,

I have to specify it in "getopts ....", right?

 

No.  You use getopts to parse the "-" options.  And when you get to the end of those, you handle the rest without getopts.

Valued Contributor

Re: ksh: optional parameter (possible use of getopts)

[ Edited ]

>> No.  You use getopts to parse the "-" options.  And when you get to the end of those, you handle the rest without getopts.

 

now i got it , a combination between "getopts" and script handling like :

 

#!/usr/bin/ksh

typeset options=""
typeset	optarg_i="TRUE"
typeset	optarg_o="TRUE"

if [ $# -ne 0 ]
then
  while getopts hio option
  do
    case "${option}" in
     i)   echo "Getopts_Option: i"
	  optarg_i="TRUE" ;
	  ;;
     o)   echo "Getopts_Option: o"
	  optarg_o="TRUE" ;
	  ;;
     h)   echo "Getopts_Option: h"
	  echo "usage $0 ...  "; exit 2
	  ;;
     [?]) echo "Getopts_Option: h"
	  echo "usage $0 ...  "; exit 2
	  ;;
    esac # case "${option}" in
  done

  # OPTIND    The index of the last option argument processed by the
  #           getopts built-in command.
  shift $(($OPTIND -1))

  echo $#
  if [ ${OPTIND} -lt $# ]
  then
    echo "${0}: Non-option present at Position \"${OPTIND}\" : $( eval echo \$${OPTIND} )"
    echo "usage $0 ...  " ; exit 2
  fi

  echo $#
  echo $*
  param="$@"
fi

exit 0

 

now it is ok ? and i put at the end only "echo"  commands for debugging

 

regards

Acclaimed Contributor Acclaimed Contributor

Re: ksh: optional parameter (possible use of getopts)

>now it is ok?

 

Well this "if" isn't right, since you have changed $# by that shift:

   if [ ${OPTIND} -lt $# ]; then

 

>param="$@"

 

And unless you use "set -A" for an array, you are collecting all of the parms into one.

Valued Contributor

Re: ksh: optional parameter (possible use of getopts)

>> Well this "if" isn't right, since you have changed $# by that shift:

>>    if [ ${OPTIND} -lt $# ]; then

 

so it is better to store $# in a variable at the beginning like cnt_args=$# and change

statement to if [ ${OPTIND} -lt ${cnt_args} ]; then

 

does it make sense ? how i check valid getopts args ?

 

regards
Acclaimed Contributor Acclaimed Contributor

Re: ksh: optional parameter (possible use of getopts)

>so it is better to store $# in a variable at the beginning like cnt_args=$#

 

Yes.  But what is that check trying to do?  What illegal input is it trying to catch?

Valued Contributor

Re: ksh: optional parameter (possible use of getopts)

> Yes.  But what is that check trying to do?  What illegal input is it trying to catch?

 

in this case it make no sense.