LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-04-2010, 08:36 PM   #1
Fatking
LQ Newbie
 
Registered: Dec 2010
Posts: 1

Rep: Reputation: 0
Need help with getops


Greetings,

I need some help with using getops in the bash shell. I want to be able to use multiple options at the same time and some of those options will require an argument and some will not.

Here is my bash script:

#!/bin/bash

while getopts ":a:bc:de:fg:" opt ; do
case $opt in
a)
echo "-a was triggered, Argument: $OPTARG" >&2
;;
b)
echo "-b was triggered" >&2
;;
c)
echo "-c was triggered, Parameter: $OPTARG" >&2
;;
d)
echo "-d was triggered" >&2
;;
e)
echo "-e was triggered, Argument: $OPTARG" >&2
;;
f)
echo "-w was triggered" >&2
;;
g)
echo "-g was triggered, Argument: $OPTARG" >&2
;;

\?)
echo "-$OPTARG is not a valid option" >&2
exit 1
;;

echo "-$OPTARG must have an argument." >&2
exit 1
;;
esac
done


This is my problem:

Fatking@home~>./a.bash -a -b
-a was triggered, Argument: -b


I ran my script with two options. The first option -a, requires an argument. However, instead of giving me an error reporting that option -a requires an argument it takes option -b as the argument though that is an option....

FatKing@home~> ./a.bash -z -b
-z is not a valid option


Fatking@home~> ./a.bash -a -z
-a was triggered, Argument: -z

FatKing@home~> ./a.bash -a
-a must have an argument.


If I only use option -a with no arguments, the script works...

If someone can help me fix this problem that would be much appreciated.
 
Old 12-04-2010, 09:46 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Try having a look at the following previous discussion on the same topic:

http://www.linuxquestions.org/questi...ameter-840133/
 
  


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



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

All times are GMT -5. The time now is 11:14 PM.

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