LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash : getopts problem in bash script. (https://www.linuxquestions.org/questions/programming-9/bash-getopts-problem-in-bash-script-708595/)

angel115 03-02-2009 09:53 AM

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.

bgeddy 03-02-2009 10:46 AM

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.

angel115 03-02-2009 10:53 AM

Problem SOLVED
 
Quote:

Originally Posted by bgeddy (Post 3462513)
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. :cool:

We can even go simple: (which after testing work fine as well)
Code:

while getopts "f:p:dga" OptionArgument; do
Angel.


All times are GMT -5. The time now is 10:22 PM.