LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-18-2011, 06:01 AM   #1
akhand jyoti
LQ Newbie
 
Registered: Feb 2011
Posts: 12

Rep: Reputation: 1
Smile getopts is not considering (skipping) one of the options


Hi,
I am using getopts in a while loop for different options I have in a function:
Here have part of my code:
funct()
{

var1="N"
var2=''

while getopts ":b:cma" opt
do
case $opt in
b)body=$OPTARG;;
c)change=$OPTARG;l_INF="Y";;
p)..
m)..
a)...
\?)...
esac
done
}

and I am calling the function from other script .
when I am calling the function for the first time (in other script )

funct -b "<something>" -c "<something>" -a
the body variable in b)body=$OPTARG;; is populating correctly but afterwards whenever I am calling the funct

funct -b "<XXXX>" -c "YYYY" -a

the -b option is getting ignored by getopts and it is OK with other options.I tried changing the order of options ,it is also did't work.
What could be the possible reason?

I want some help on this getopts :
1) does it matter the order of options?
2) How the value of $OPTIND(option index) get assigned? I mean is it like in the order of calling in function or in the order in which it is defined in getopts
getopts ":b:cma" i.e. b has OPTIND=2,for c=3 or just it is just in any value>1?

Thanks :-)
its =colon : then p

Last edited by akhand jyoti; 11-18-2011 at 06:04 AM. Reason: its :p =colon : then p
 
Old 11-18-2011, 06:20 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by akhand jyoti View Post
when I am calling the function for the first time (in other script )

funct -b "<something>" -c "<something>" -a
the body variable in b)body=$OPTARG;; is populating correctly but afterwards whenever I am calling the funct

funct -b "<XXXX>" -c "YYYY" -a

the -b option is getting ignored by getopts and it is OK with other options.I tried changing the order of options ,it is also did't work.
What could be the possible reason?

I want some help on this getopts :
1) does it matter the order of options?
2) How the value of $OPTIND(option index) get assigned? I mean is it like in the order of calling in function or in the order in which it is defined in getopts
getopts ":b:cma" i.e. b has OPTIND=2,for c=3 or just it is just in any value>1?

Thanks :-)
IDK why it is not working; strange that only the -b option is affected. Some debugging might help:
Code:
funct()
{
    echo "DEBUG: ${FUNCNAME[ 0 ]} called with $*"
    var1="N"
    var2=''

    while getopts ":b:pc:ma" opt
    do
        echo "DEBUG: opt: $opt, OPTARG: $OPTARG"
        case $opt in
            b)body=$OPTARG;;
            c)change=$OPTARG;l_INF="Y";;
            p)..
            m)..
            a)...
            \?)...
        esac
    done
}
1) does it matter the order of options? No

2) How the value of $OPTIND(option index) get assigned? From the GNU Bash Reference: "When the end of options is encountered, getopts exits with a return value greater than zero. OPTIND is set to the index of the first non-option argument". AFAIK the only use for OPTIND is to shift the options and option arguments out of the way to get the arguments like:
Code:
shift $(( $OPTIND-1 ))
if [[ $* != '' ]]; then
    <whatever you like with the remaining arguments which are available in "$@">
fi

Last edited by catkin; 11-18-2011 at 06:21 AM. Reason: speeling
 
1 members found this post helpful.
Old 11-18-2011, 06:26 AM   #3
davemguru
Member
 
Registered: Apr 2006
Location: London
Distribution: Pclos,Debian,Puppy,Fedora
Posts: 87

Rep: Reputation: 42
getopts was designed to process arguments on the commandline.
However functions get their arguments in "$@" - same as the commandline.
But, because of the original intent of the design - the global variables OPTIND and OPTARGS don't get reset.

from the manual
Quote:
OPTIND and OPTARG are local to the shell script. If you want to export them, you must do so explicitly. If the script invoking getopts sets OPTIND to 1, it can call getopts again with a new set of parameters, either the current positional parameters or new arg values.
or you could
Code:
unset $OPTSTRING
unset $OPTIND
etc...
at the end of your function.

Dave



If I have helped - can you "click my rep" and indicate that I have done so.
 
1 members found this post helpful.
Old 11-18-2011, 02:44 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Please use [code][/code] tags around your code, to preserve formatting and to improve readability.

In the advanced reply screen you can also tell it to turn off smilies, if they conflict with something in your post.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help with getopts paragkalra Programming 4 03-26-2011 10:17 PM
getopts kj6loh Programming 4 09-10-2009 04:53 PM
question on getopts tostay2003 Programming 1 12-25-2007 05:39 AM
Passing command line arguments through getopts and long options neville310 Programming 3 04-16-2007 07:38 AM
FC2 test 3 mouse skipping a beat, movie skipping also jang Fedora 1 10-28-2004 08:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration