LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 03-02-2009, 09:53 AM   #1
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 493

Rep: Reputation: 75
bash : getopts problem in bash script.


Hello There,

I'm having an issue using getopts in my bash script.
For trouble shooting purposes I've make it simple:

Code:
#!/bin/bash

while getopts ":f:p:d:g:a" OptionArgument; do
case $OptionArgument in
	
	p ) productdb=${OPTARG};;
	d ) debug="1";;
	f ) cdtFile=${OPTARG};;
	g ) generic="1";;
	a ) a="just another one just for test";;
	\?) echo = "No option specify";;
	* ) echo = "wrong variable";;
esac
done


echo "product = ${productdb}"
echo "cdtFile = ${cdtFile}"
echo "debug = ${debug}"
echo "generic = ${generic}"
echo "a= ${a}"
In my example p and f will be optional and a is just for testing.

below is an example of what I type and the output:
Code:
$ ./1_analysing -d -g -a -pimsx -ffileName 
product = imsx
cdtFile = fileName
debug = 1
generic = 
a= just another one just for test
the generic value stay empty which I don't why.

I've found many example on internet but none of them are working as expected.
Is there a bug some where or did I miss something?

Thanks for your help;
Angel.

Last edited by angel115; 03-02-2009 at 09:54 AM.
 
Old 03-02-2009, 10:46 AM   #2
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 227Reputation: 227Reputation: 227
Try it with this change :
Code:
while getopts ":f:p:dga" OptionArgument; do
From getopts help :
Quote:
if a letter is followed by a colon, the option is expected to have an argument, which should be separated from it by white space.
 
Old 03-02-2009, 10:53 AM   #3
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 493

Original Poster
Rep: Reputation: 75
Thumbs up Problem SOLVED

Quote:
Originally Posted by bgeddy View Post
Try it with this change :
Code:
while getopts ":f:p:dga" OptionArgument; do
From getopts help :

Hehe, you save my life thank you so much, my script is working like a charm now.

We can even go simple: (which after testing work fine as well)
Code:
while getopts "f:p:dga" OptionArgument; do
Angel.

Last edited by angel115; 03-02-2009 at 11:09 AM.
 
  


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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Problem with a bash script sloteel Programming 4 02-09-2009 02:12 PM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 06:00 AM
[bash] having trouble debugging this bash script. jons Programming 4 02-08-2007 06:51 AM
Problem in script bash with if nadavvin Linux - General 2 08-05-2006 01:51 PM


All times are GMT -5. The time now is 11:45 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration