Question about getopts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question about getopts
# 1  
Old 11-14-2010
Question about getopts

I am trying to use the finction getopts in script.

From what I understand this can be an example for using:
Code:
while getopt "p:rvt:" 
do 
         case  
                  p)
                       echo "$OPTARG"
                        ;;
                   r)
                       echo "example"
                       ;;
                        
                   v)
                       echo "example "
                       ;;
                   t)
                        echo  "$OPTARG
                       ;;
             esac
done

Now I understand that if there are : after the letter then it mean that it expect argument when this letter appear.

I want to know how can I handle a case in which the user send none of the parameters above.
I mean for example when he did not write "-p","-r","-v","-t"
# 2  
Old 11-14-2010
My preferred way to handle this is to first include a ':' as the first character in getopts...

Code:
while getopts ":dhqTv" OPT                              # While there is a command line option

This tells getopts not to print an error message when an unknown option is used.

Then, you have to provide your own message when an unknown option appears, use '?' in the case statement:

Code:
      ?)   print_status_message "ERROR: Unknown flag encountered.\n" ERROR
           print_options
           exit -1
           ;;

If you omit the first ':' the output is like:

Code:
root@ms:/home/unxsa/bin>./new_script_header.ksh93.getopts.unix -k

./new_script_header.ksh93.getopts.unix: -k: unknown option.

ERROR: Unknown flag encountered.

This User Gave Thanks to purdym For This Post:
# 3  
Old 11-15-2010
Quote:
Originally Posted by purdym
My preferred way to handle this is to first include a ':' as the first character in getopts...

Code:
while getopts ":dhqTv" OPT                              # While there is a command line option

This tells getopts not to print an error message when an unknown option is used.

Then, you have to provide your own message when an unknown option appears, use '?' in the case statement:

Code:
      ?)   print_status_message "ERROR: Unknown flag encountered.\n" ERROR
           print_options
           exit -1
           ;;

If you omit the first ':' the output is like:

Code:
root@ms:/home/unxsa/bin>./new_script_header.ksh93.getopts.unix -k

./new_script_header.ksh93.getopts.unix: -k: unknown option.

ERROR: Unknown flag encountered.

thanks, but do you handle case in which he choose no option at all and you want to send error message for this?

Because I want to write a script that when the user give no argument at all, s/he will get error message.
# 4  
Old 11-15-2010
Try something like:

Code:
print_options ()
{
   cat <<EOF

   Usage:${PROGRAM_NAME##*/}

   Optional Parameters:
           [[-d] [-v] | [-q]]

EOF
exit 0

}

...

###################################################################################
# this short curcuit is needed for when no parameters are given
###################################################################################
if [[ $1 == "" ]]
then
   print_options

fi

...

while getopts ":dhqTv" OPT                              # While there is a command line option
do
   case $OPT in
      d)   DEBUG_STRING=" -d "                                  # this is used to pass this flag to another script
           let "DEBUG += 1"
           (( $DEBUG >= 2 )) && print_status_message "DEBUG: DEBUG=$DEBUG" DEBUG
           dFLAG=$TRUE
           (( $DEBUG >= 2 )) && print_status_message "DEBUG: dFLAG=${TFSTATUS[$dFLAG]}" DEBUG
           ;;

      h)   print_options;;

      q)   QUIET_STRING=" -q "                                  # this is used to pass this flag to another script
           QUIET=$TRUE
           qFLAG=$TRUE
           (( $DEBUG >= 2 )) && print_status_message "DEBUG: qFLAG=${TFSTATUS[$aFLAG]}" DEBUG
           ;;

      T)   TESTING_STRING=" -T "                                # this is used to pass this flag to another script
           TESTING=$TRUE
           TFLAG=$TRUE
           (( $DEBUG >= 2 )) && print_status_message "DEBUG: TFLAG=${TFSTATUS[$TFLAG]}" DEBUG
           ;;

      v)   VERBOSE_STRING=" -v "                                # this is used to pass this flag to another script
           let "VERBOSE += 1"
           (( $DEBUG >= 2 )) && print_status_message "DEBUG: VERBOSE=$VERBOSE" DEBUG
           vFLAG=$TRUE
           (( $DEBUG >= 2 )) && print_status_message "DEBUG: vFLAG=${TFSTATUS[$vFLAG]}" DEBUG
           ;;

      ?)   print_status_message "ERROR: Unknown flag encountered.\n" ERROR
           print_options
           exit -1
           ;;

   esac
done                                            # end -

This User Gave Thanks to purdym For This Post:
# 5  
Old 11-15-2010
Never been a big fan of getopt in KSH, its very limited. What happens if
you have an option that you want to use the same letter for?

For example, db_id and db_password. You can't use -d for both of them right.

Therefore, I take advantage of KSH and do something like this that lets
you pass in more descriptive option names. Note, case does not make a
difference if you code like this.

Code:
 
while(($#))
do
   case $1 in
       -+([dD]*(b_id|B_ID)))
         shift
         DB_ID=$1
         ;;
       -+([dD]*(b_pass|B_PASS)))
         shift
         DB_PASS=$1
         ;;
    -+([fF]*(lag|LAG)))
          shift
          let flag_passed=1
         ;;
       -+([iI]*(nfo|NFO)))
         usage
         return 0
         ;;
      *)
         shift ;;
   esac
done

This allows you to call your script like this

script-name -db_id xxx -db_password mypassword parm1 parm2 .....

Note if you don't want to pass a variable and set a flag only look at -flag
This User Gave Thanks to BeefStu For This Post:
# 6  
Old 11-17-2010
thank you both.

However I am not sure it solve my problem but I think I don't explain myself good enough.

I will come back with a better specification of the problem.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Question about getopts optional argument [args...]

There are many places where I can see the syntax description for optargs, which, usually boils down to this: getopts OPTSTRING VARNAME where: OPTSTRING tells getopts which options to expect and where to expect arguments VARNAME tells getopts which shell-variable to use for option reporting... (2 Replies)
Discussion started by: sharkura
2 Replies

2. Shell Programming and Scripting

Using getopts

Hi. Can somebody please show me an example of how to use getopts to assign a variable if it's been passed into the script but to set a default if no value has been passed in? And also how to handle a param with multiple values ... so a sub parse (can I use a function for this?)? Here's my code... (1 Reply)
Discussion started by: user052009
1 Replies

3. Shell Programming and Scripting

Getopts help

Hi All, I am writing a script to pass the getopts argument to the function which I have. But it as soon as I execute the script, the argument is taking it as blank. I tried using multiple way to check but its not working. Can someone please let me know what wrong in this code. function1()... (4 Replies)
Discussion started by: sidh_arth85
4 Replies

4. Shell Programming and Scripting

? used in getopts

Suppose I have a code below . while getopts a: opt do case $opt in a) app_name="$OPTARG";; *) echo "$opt is an invalid option"; exit 1;; ?) echo "The value of $OPTARG is an invalid option"; exit 1;; esac done Could anyone please tell me in which case my... (1 Reply)
Discussion started by: maitree
1 Replies

5. Shell Programming and Scripting

getopts question

I am trying to set up prompts when you don't enter the right information or dont enter the information at all, when executing a script. Below is the question that i am asking and i am not sure how to set up the if statements to make sure that the user enters the name, cpu's, memory and ip. I was... (3 Replies)
Discussion started by: rookieuxixsa
3 Replies

6. Shell Programming and Scripting

getopts question

could anyone please tell me what this will automatically set my variable all=True.... #!/bin/sh all=FALSE while getopts a: option do case "option" in a) all=TRUE;; /?) echo "...... " exit 1;; esac done if then echo "true" else echo "false" (2 Replies)
Discussion started by: k2k
2 Replies

7. Shell Programming and Scripting

getopts help

Hi i have part of the scripts below ,getopt for -h or ? not working for me. can anybody tell me if this sytax right or wrong. #!/usr/bin/ksh program=$(basename $0) ##################################################################################### function usageerr { RC=1 ... (3 Replies)
Discussion started by: GrepMe
3 Replies

8. Shell Programming and Scripting

getopts question!!!

is there a better way to check if all args are set??? while getopts h:p:u: opt do case "$opt" in h) host="$OPTARG";; p) port="$OPTARG";; u) user="$OPTARG";; \?) echo >&2 \ "usage: $0 -h host -p port -u user" exit 1;; esac done ... (1 Reply)
Discussion started by: andy2000
1 Replies

9. Shell Programming and Scripting

question about getopts

hello there I am back with more questions (sorry it is been quite a while since I had done scripting). I had tried the search function to search for the threads that might have an answer to my question, but I could not find it, so I had decided to post it. I had created the scripts below in ksh... (2 Replies)
Discussion started by: ahtat99
2 Replies

10. Shell Programming and Scripting

help in getopts

hey need help with getopts again. i am using getopts to read my command line options and arguments. i can manage to do for options that have only one argument e.g srcipt_name -f 3 i am able to use getopts to do this but i am having problems two accept more than two agruments e.g.... (1 Reply)
Discussion started by: problems
1 Replies
Login or Register to Ask a Question