Register
It is currently Thu Aug 21, 2014 3:59 pm

Problem with getops


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Wed Nov 23, 2011 9:19 am   

Joined: Wed Nov 23, 2011 9:11 am
Posts: 3
Hi all,

I have a weird problem with my script..

So, when i try to use my script:

Code:
./myscript.sh -i value -b


The script dont update CONVERSION...

But if I enter this:

Code:
./myscript.sh -b -i value


The script update CONVERSION

Code:
while getopts "hbi:c:" OPTION
do
        case $OPTION in
                h) 
                        usage
                        ;; 
                i) 
                        set $OPTARG
                        ;; 
                b) 
                        CONVERSION=1
                        ;; 
                c) 
                        calculate
                        ;; 
        esac
done


I dont understard where is my mistake on my code?

Thanks in advance.


Top
 Profile  
 PostPosted: Wed Nov 23, 2011 9:34 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 535
hi,

set probably doesn't do what you think.
Code:
help set
Set or unset values of shell options[…]


Top
 Profile  
 PostPosted: Wed Nov 23, 2011 9:39 am   

Joined: Wed Nov 23, 2011 9:11 am
Posts: 3
set do what I want.

My problem is if "-b" isnt the first parameter my script dont update CONVERSION to 1
But if -b is the first parameter my script update CONVERSION to 1

Maybe, in bash an option without parameter must be first?


Top
 Profile  
 PostPosted: Wed Nov 23, 2011 9:57 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 535
set acts on positionnal parameters, so getopts loses them.


Top
 Profile  
 PostPosted: Thu Nov 24, 2011 4:23 am   

Joined: Wed Nov 23, 2011 9:11 am
Posts: 3
Ok, i changed how to affect my value with -i and no problem.

Thx for help

Best regards,


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP