Operating System - HP-UX
1753819 Members
9962 Online
108805 Solutions
New Discussion

Re: ksh: using of getopts and checks of wrong inputs

 
SOLVED
Go to solution
support_billa
Valued Contributor

ksh: using of getopts and checks of wrong inputs

hello,

 

i am using getopts and tried to prevent wrong inputs :

 

example :

 

 while getopts ':c:f:m:eht' option
  do
    case "${option}" in
         c)   OPTION_C="${OPTARG}" ;;
         f)   OPTION_F="${OPTARG}" ;;
         m)   OPTION_M="${OPTARG}" ;;
         s)   OPTION_S="Y" ;;
         e)   OPTION_E="J" ;;
         t)   OPTION_T="J" ;;
         h)   echo show_Usage  ; exit 2;;
         [?]) echo show_Usage  ; exit 2;;
    esac

done

 

in the attachment is my getopts test script.

 

allowed should be :

./getopts_test.sh -f <value> -m <value> -c  <value> -t

or

./getopts_test.sh -f <value> -m <value> -c  <value> -e

 

but not allowed options "-t" and "-e" , only "-t" or "-e"

 

i tried to prevent this issue, but when you specify a value after "-e" or "-t" , then my script can't handle this :

 

OK
./getopts_test.sh -f Option_F -m 10240 -c Option_C -t
./getopts_test.sh -f Option_F -m 10240 -c Option_C -e

WRONG
./getopts_test.sh -f Option_F -m 10240 -c Option_C -e -t

./getopts_test.sh -f -m -c -t -e

ALLOWED but WRONG
./getopts_test.sh -f Option_F -m 10240 -c Option_C -t Option_T -e Option_E

 

possibilty of DEBUG
DEBUG=ON ./getopts_test.sh -f Option_F -m 10240 -c Option_C -t Option_T -e Option_E

 

regards

5 REPLIES 5
Dennis Handly
Acclaimed Contributor

Re: ksh: using of getopts and checks of wrong inputs

>ALLOWED but WRONG
>./getopts_test.sh -f Option_F -m 10240 -c Option_C -t Option_T -e Option_E

 

So this is the only problem in the script, it allows this?

 

The following doesn't seem to be consistent, why sleep?  Why missing show_Usage for the first?

You also repeat "-e" in each error message.

 

if [ "${OPTION_E}" = "J" -a  "${OPTION_T}" = "J"  ]; then
    echo "${SCRIPT}:  Option \"-e\" and Option \"-e\" isn't allowed!"
    exit 2
fi
if [ "${OPTION_E}" = "N" -a  "${OPTION_T}" = "N"  ]; then
    echo "${SCRIPT}:  Option \"-e\" or Option \"-e\" must be set!"
    sleep 1
    show_Usage
    exit 2
fi

 

Also due to fuzziness of the English language, you might want to beef up what "and" and "or" really mean.  They don't mean the same as "&&" or "||".

What you really mean for BOTH:

'Options -e and -t are mutually exclusive and one is required'

support_billa
Valued Contributor

Re: ksh: using of getopts and checks of wrong inputs

So this is the only problem in the script, it allows this?

yes,this is the only problem !

What you really mean for BOTH:
'Options -e and -t are mutually exclusive and one is required'

 Only one Option is allowed and should exists  and not :   .... "-e -t " or "-e value " or "-t value"

This is what i mean : 'Options -e and -t are mutually exclusive and one is required' sorry for my bad english explanation .

can "getopts" handle this ? i tried to handle it, with variables

regards,

 

 

Dennis Handly
Acclaimed Contributor
Solution

Re: ksh: using of getopts and checks of wrong inputs

>yes,this is the only problem!

 

After your while loop you need something like:

if [ ${OPTIND} -le $# ]; then
   echo "Non-option present at ${OPTIND}"
   show_Usage; exit 2
fi

>Can "getopts" handle this? I tried to handle it, with variables

 

No, if options are required, optional, mutually exclusive or duplicates are not allowed, you must handle that yourself.

support_billa
Valued Contributor

Re: ksh: using of getopts and checks of wrong inputs

hello,

 

if [ ${OPTIND} -le $# ]; then
   echo "Non-option present at ${OPTIND}"
   show_Usage; exit 2
fi

handle wrong options  like "-t Option_T", right ?

 

OK with your input ${OPTIND}

DEBUG=ON ./getopts_test.sh -f Option_F -m 10240 -c Option_C -t Option_T -e Option_E

 Output: getopts_test.sh: Non-option present at 8

OK and i handle it in the programm

DEBUG=ON ./getopts_test.sh -f Option_F -m 10240 -c Option_C -t -e

Output: getopts_test.sh:  Option "-e" and Option "-e" isn't allowed !

 

OK and i handle it in the programm

DEBUG=ON ./getopts_test.sh -f -m -c -t -e

 Output: USAGE: getopts_test.sh  -f Value_Option_F
                        -m Value_Option_M (Digit)
            -c Value_Option_C { -e | -t}

 

Summary : ${OPTIND} handle wrong options and other checks i have to do with program steps

Dennis Handly
Acclaimed Contributor

Re: ksh: using of getopts and checks of wrong inputs

>handle wrong options  like "-t Option_T", right?

>${OPTIND} handles wrong options and other checks I have to do with program steps

 

Actually these are separate.  There is a "-t" and some non-option "Option_T", even if the latter is not after the "-t".

At the end of getopts, ${OPTIND} points to where it stopped.