question about getopts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting question about getopts
# 1  
Old 08-19-2006
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 (assume the ksh file name is abc.ksh)
--------------------------------------------------------------------------
var1=' '
var2=' '

while getopts ":aSmilie" opt
do
case $opt in
a) var1=$OPTARG;;
b) var2=$OPTARG;;
\?) echo "Invalid argument"; exit 1;;
esac
done
shift 'expr ${OPTIND} - 1'

echo "Statement"
--------------------------------------------------------------------------

I ran the codes
--------------------------------------------------------------------------
Server temp-> abc.ksh -a some_value -b other_value
--------------------------------------------------------------------------

It works perfect, but if I ran the code with out the command line option
--------------------------------------------------------------------------
Server temp-> abc.ksh some_value other_value
Statement
--------------------------------------------------------------------------

I thought if there are no -a or -b option is given, getopts would automatically faults out and abc.ksh would just display "Invalid Argument" and exit. Did I not doing this section of scripts correctly or I am just plain missing something?

Thank you for your time
# 2  
Old 08-20-2006
It would fault out if you entered abc.ksh -c somevalue
because c is not in the choices getopts has. It only checks characters like -<character>

You can use $# to return the number of command line arguments, so for example,
Code:
if [[ $# -lt 3 ]] ; then
   echo "Invalid arguments"
   exit 1
fi

Change the 3 above into whatever number you want.
[/code]
# 3  
Old 08-20-2006
yes that would work, but the thing what if my users forgot to use the options, such as just using it as "abc.ksh some_value". Because I thought I had set the error at the last line, when "abc.ksh" is call without options, it would return invalid argument and exit. But it had executed the next instruction instead.

For example if I execute the abc.ksh without using command line option,
--------------------------------------------------------------------------
Server temp-> abc.ksh some_value other_value
--------------------------------------------------------------------------

I would expect the folloing output
--------------------------------------------------------------------------
Invalid argument
--------------------------------------------------------------------------

Instead I got the following
--------------------------------------------------------------------------
Statement
--------------------------------------------------------------------------

I was thinking to use this abc.ksh with options, if user did not key in the correct option, I would only print the error as well as the usage and then exit the abc.ksh. But when I executed this scripts, if user start using this without the correct option, it still execute the next line then exit.

So I am wondering if I had messed up my logic or I did not use this option correctly.
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

Question about getopts

I am trying to use the finction getopts in script. From what I understand this can be an example for using: while getopt "p:rvt:" do case p) echo "$OPTARG" ;; r) echo... (5 Replies)
Discussion started by: programAngel
5 Replies

6. 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

7. 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

8. 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

9. 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

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