Order of getopts parameters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Order of getopts parameters
# 1  
Old 01-04-2013
Order of getopts parameters

Hi,
Does the order of argument and non-argument command line parameters matter to getopts?

with a getopts line in my script of
getopts p:cs opt

a command line of <script> -p 5 -s
only picks up the -p option, while <script> -s -p 5 picks up both.

Removing the space between the p and the argument doesn't help, nor does reordering the string in the getopts command.

I'm using Solaris 5, and ksh.

Thanks
# 2  
Old 01-04-2013
Try the option and value without space.
Code:
 <script> -p5 -sString

# 3  
Old 01-04-2013
works fine for me...
test.ksh:
Code:
#!/bin/ksh
while getopts "p:cs" opt  ; do
        case $opt in
                p) echo opt-p: $OPTARG ;;
                c) echo opt-c ;;
                s) echo opt-s ;;
        esac
done

Code:
$ ./test.ksh -s -p 5
opt-s
opt-p: 5

# 4  
Old 01-04-2013
Thanks for both responses, but as I said in my original post, removing the space doesn't help, and putting the no-arg option first works fine. It's putting the -s after the -p5 (or -p 5) that doesn't.
Subbeh, can you try running ./test.ksh -p 5 -s and see what you get?

Thanks.


Never mind. Subbeh's test script works for me when I reverse the order. It must be something else in my script.
I'll investigate further.

---------- Post updated at 02:06 PM ---------- Previous update was at 11:51 AM ----------

OK, my C programming knowledge was tripping me. I was putting "break"s at the end of each case, which of course took me right out of the "while" loop.
Anyway, thanks for playing.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getopts with optional parameters

Hi Unix Gurus, i am on learning path of unix, and yet to discover many things. I came across with this requirement where i need to pass parameters but the position of parameters is not fixed so after doing some google search got to know "getopts" can handle that. So here is my code: function... (3 Replies)
Discussion started by: gnnsprapa
3 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

getopts help

First off, I apologize for my lack of knowledge. I realize my problem will probably seem pretty basic to everyone, but I've been at this for several hours now and I've gotten nowhere. I would contact my professor, but it is too late for that. Anyway, I'm trying to write a function called... (1 Reply)
Discussion started by: Unknown50862
1 Replies

4. Programming

Parse parameters with getopts

Hi, this is my problem I have script with two parameters -n name and -s surname. Both have arguments and I want to know how parse these parameters with getopts. When you write ./names -n John -s White it find you all persons, which name is John White, but when you write ./names -n John ... (1 Reply)
Discussion started by: frees
1 Replies

5. AIX

tuning network parameters : parameters not persist after reboot

Hello, On Aix 5.2, we changed the parameters tcp_keepinit, tcp_keepintvl and tcp_keepidle with the no command. tunrestore -R is present in inittab in the directory /etc/tunables we can clearly see the inclusion of parameters during reboot, including the file lastboot.log ... (0 Replies)
Discussion started by: dantares
0 Replies

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

7. Shell Programming and Scripting

Using getopts

I am having some trouble/questions with getopts that I can't find any solid info on with google I need it to parse things of the syntax of: -r # # # -f -c with as many repeats as possible, and it should catch erroneous commands also, but continue going... my first question is, -r... (3 Replies)
Discussion started by: TurboArkhan
3 Replies

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

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

10. Shell Programming and Scripting

getopts takes options for parameters

Here is my post with a question about getopts. I am running korn shell on Solaris 5.8. I am trying to ensure that certain options require a parameter, which is easy enough. I have found that if multiple options are entered on the command line together, but the parameter for one of the options is... (1 Reply)
Discussion started by: UCD-Randy
1 Replies
Login or Register to Ask a Question