LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-10-2009, 03:32 PM   #1
kj6loh
Member
 
Registered: Jun 2004
Posts: 47

Rep: Reputation: 17
getopts


How do you read multiple arguments with getopts? The following is a test case:

$ cat test.sh
#!/bin/bash
ECHO=/bin/echo
while getopts "hmq?" opt; do
case "$opt" in
"m") MYWAY=1;
$ECHO "MYWAY"
shift;
;;
"q") QUIET=1;
$ECHO "QUIET"
shift;
;;
"h" | "?")
$ECHO "Usage: $0 [-m| -q]"
exit 0
;;
esac
done
shift $(($OPTIND - 1))
$ ./test.sh -m
MYWAY
$ ./test.sh -q
QUIET
$ ./test.sh -q -m <-- Shouldn't it read -m too?
QUIET
$ ./test.sh -t <-- Why is this caught
./test.sh: illegal option -- t
Usage: ./test.sh [-m| -q]
$ ./test.sh - <-- And not this?
$
 
Old 09-10-2009, 03:40 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977
I was getting a fair bit confused with getopt and getopts earlier in the week, I found this article really useful - http://aplawrence.com/Unix/getopts.html and ended up migrating from getopt to getopts with the help of the really simple example he gives.

For your specific questions, firstly I think as your shifting AND using the options as a while input you're jumping over it. secondly there's nothing wrong with no options by default, it's merely an empty set of data to work on.

Last edited by acid_kewpie; 09-10-2009 at 03:42 PM.
 
Old 09-10-2009, 03:45 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,577
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
You don't need the "shift"s in the getopts loop; getopts does it all for you.

When posting code, it's easier to read if you put it in code tags.
 
Old 09-10-2009, 04:50 PM   #4
kj6loh
Member
 
Registered: Jun 2004
Posts: 47

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by acid_kewpie View Post
I was getting a fair bit confused with getopt and getopts earlier in the week, I found this article really useful - http://aplawrence.com/Unix/getopts.html and ended up migrating from getopt to getopts with the help of the really simple example he gives.

For your specific questions, firstly I think as your shifting AND using the options as a while input you're jumping over it. secondly there's nothing wrong with no options by default, it's merely an empty set of data to work on.
Yep, thanks I shifted from a case to getopts as I now have more than a few
switches.
 
Old 09-10-2009, 04:53 PM   #5
kj6loh
Member
 
Registered: Jun 2004
Posts: 47

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by catkin View Post
You don't need the "shift"s in the getopts loop; getopts does it all for you.

When posting code, it's easier to read if you put it in code tags.
I was wondering where my formatting went!
 
  


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
question on getopts tostay2003 Programming 1 12-25-2007 05:39 AM
struggling with getopts mike9287 Programming 6 07-14-2006 01:42 AM
Mandatory arguments with getopts? rose_bud4201 Programming 2 03-10-2005 03:18 PM
getopts multiple parameters arnulfo Programming 2 12-23-2004 06:12 AM
Help with getopts command Rezon Programming 3 10-22-2003 05:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:12 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