LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 02: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, 02:40 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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 02:42 PM.
 
Old 09-10-2009, 02:45 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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, 03: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, 03: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



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

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

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