LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 06-04-2018, 01:19 AM   #1
kartik23
LQ Newbie
 
Registered: Jun 2018
Location: India
Distribution: Arch,Fedora
Posts: 3

Rep: Reputation: Disabled
Post Why does the getopt needs the -- case in switch statement?


So while looking on getopt i fount out that there needs to be an extra case for -- even though i dont want parameters in the format like -flag -flag -- argument1 argument2 etc.Any specific reason for why getopt need a special case for --. Just curious.
Sample Script:-

1 set -- $(getopt -q ab:c "$@")
2 while [[ -n "$1" ]]
3 do
4 case "$1" in
5 -a) echo "Found the -a option" ;;
6 -b) param="$2"
7 echo "Found the -b option, with parameter value $param"
8 shift 2;;
9 -c) echo "Found the -c option" ;;
10 --)shift
11 break;;
12 *) echo "$1 is not an option";;
13 esac
14 shift
15 done

Last edited by kartik23; 06-04-2018 at 01:20 AM.
 
Old 06-04-2018, 03:41 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,802

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
getopt does not need --. But probably I misunderstood your question.
-- is a special flag and is passed to set, not getopt. The meaning of -- is: do not take all the arguments (on the right side of --) as options even if they started with -.
see man bash and look for --
 
1 members found this post helpful.
Old 06-04-2018, 03:44 AM   #3
kartik23
LQ Newbie
 
Registered: Jun 2018
Location: India
Distribution: Arch,Fedora
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
getopt does not need --. But probably I misunderstood your question.
-- is a special flag and is passed to set, not getopt. The meaning of -- is: do not take all the arguments (on the right side of --) as options even if they started with -.
see man bash and look for --
Sorry if you got confused but i was actually talking about the -- that is present in switch statement. The --) case. But now i understood why we need it there thanks for your help.

Last edited by kartik23; 06-04-2018 at 03:47 AM.
 
Old 06-04-2018, 03:52 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,802

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
getopt itself will add a --. You can check the command:
Code:
getopt -q ab:c <args>
and you will see the result will contain an additional -- (again, left side the options, right side the additional arguments).
in your case therefore you need to handle that -- too.

Insert set -xv at the beginning of your script and you will see what's really happening.
 
1 members found this post helpful.
Old 06-04-2018, 03:59 AM   #5
kartik23
LQ Newbie
 
Registered: Jun 2018
Location: India
Distribution: Arch,Fedora
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
getopt itself will add a --. You can check the command:
Code:
getopt -q ab:c <args>
and you will see the result will contain an additional -- (again, left side the options, right side the additional arguments).
in your case therefore you need to handle that -- too.

Insert set -xv at the beginning of your script and you will see what's really happening.
Using getopt for a while but was remembering that case. Most of the people advised getopts without explaining the why now i know the why thanks for your help again.
 
  


Reply



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
[SOLVED] Anyone know a better means to track argc in case using getopt? BW-userx Programming 11 08-01-2017 02:34 PM
CASE statement oracledb0101 Linux - Newbie 3 06-13-2012 03:15 PM
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 04:50 AM
Case Statement craigjward Programming 6 12-12-2007 11:33 PM
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 08:05 PM

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

All times are GMT -5. The time now is 04:15 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
Open Source Consulting | Domain Registration