getopts issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getopts issue
# 1  
Old 03-28-2011
getopts issue

Hi,
I have a script, that accepts two positional parameters. Now, I want to add one more optional parameter to it. But I do not want it to be an optional positional parameter as this will impact the future enhancement to the script. Hence I want to use getopts.

The script is now executed as follows:
Code:
./scriptname arg1 arg2 no_exit (where no_exit is the optional 3rd positional parameter)

I now want to execute the script as follows:
Code:
./scriptname arg1 arg2 -n (where -n is the optional parameter)

I am using the below code:
Code:
while getopts :n option
do
  case $option in
    n) no_exit=yes
  esac
done

This works if I execute the script in as
Code:
./scriptname -n arg1 arg2

but it does not work if I execute the script as
Code:
./scriptname arg1 arg2 -n

Any idea what modification has to be done to getopts to execute the script as
Code:
./scriptname arg1 arg2 -n

Thanks in advance.
# 2  
Old 03-28-2011
getopts stop analyzing the arguments at the first one it doesn't recognize, so you need to give him only the optional arguments. This can be done with a shift.

Code:
arg1=$1
arg2=$2
shift 2
while getopts :n option
do
  case $option in
  n) no_exit=yes
  esac
done

# 3  
Old 03-29-2011
Thanks a lot. It works.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. UNIX for Dummies Questions & Answers

Getopts

while getopts v OPTION do case $OPTION in v) echo "Hello" ;; *) exit 1;; esac done Suppose I have script tmp.sh Whose Signature is tmp.sh <fixed_argument> When I run the script with tmp.sh -v "file", it echoes a hello but, when I try the other way i.e, tmp.sh... (1 Reply)
Discussion started by: Devendra Hupri
1 Replies

3. Shell Programming and Scripting

Using getopts. Need help

Hi all... I have been looking on here for the past few days for an answer and Im gonna have to break down and ask. I just learned about the getopts command last week so have been trying to utilize it in my scripts. Below, I am trying to set up a case structure for options using getopts.... (1 Reply)
Discussion started by: losingit
1 Replies

4. UNIX for Dummies Questions & Answers

Issue with getopts

Hi, I am using below code to check files on ftp server and notify users if files are not arrived. I am invoking another script to send mail using smtpmail. Mail is being sent properly in first call to child script notify.sh ( i.e. in if condition ) but in second call ( i.e. in else part ) mail... (5 Replies)
Discussion started by: madhukalyan
5 Replies

5. UNIX for Dummies Questions & Answers

Getopts

Hey, i need help with the use of getopts in shell script. tried reading a lot online, but found incomplete examples (maybe complete but cudn't make out). PLzz help...explain in deatil plzzz, i am a newbie:confused: (3 Replies)
Discussion started by: SasankaBITS
3 Replies

6. Shell Programming and Scripting

using getopts

Hi, I have a program where I want to use getopts. I want to use "-i" option and then optionally supply arguments. If user dosent supply arguments, then also it should work. Please tell me how to proceed. Here is some code, this is not right code btw but a sample to understand what I want to... (1 Reply)
Discussion started by: som.nitk
1 Replies

7. HP-UX

using getopts

Is there a restriction on levels of using 'getopts' ? I have several scripts, each of which requires an option as the first parameter . If I call one prg separately it works fine, but when one prg calls another prg and passes the option on the called prg, then the called prg seems not to process... (3 Replies)
Discussion started by: vslewis
3 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

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

10. Shell Programming and Scripting

getopts

I have a script which fires a command based on certain parameters. I am posting the code below.. The options needs be given such that -u option goes along with -d and -s, -f goes with -d and -t goes with -s and -d. 1) How do I ensure that user misses any of the option then he should be... (5 Replies)
Discussion started by: yerra
5 Replies
Login or Register to Ask a Question