BASH script using getopt to parse optional arguments
i have the following scenario want to run the following script with manadory and optional argumnets
Manadory options are :
type -t [A/B]
balances -b bal
./test filename port -t A -b bal
my code i have that won't parse the options is
This is what i have
while getopts b:t:p opt
case $opt in
if [ "$BAL" == "" ]
echo "Balances is " $BAL
?) print_usage $0
echo "Balances: "$BAL
echo "Type: "$TYPE
First idea is to use CODE tags to make your code more readable; you can most easily use them by changing to advanced mode posting and using the # button.
Copying your code into CODE tags:
Guessing what the problem is, getopts is traditional; it must have the options (and option arguments) before the arguments. Try
You also have to shift the parameters after processing the optional arguments so you can grab what's left.
Edit: Thinking about it, It is possible instead to have the mandatory arguments first, if you really want it that way. Just reverse the above: set filename and port first, then shift the parameters by two so that the getopts loop can process the rest.
The thing you can't do is mix the two types arbitrarily. The getopts arguments pretty much have to come together as a group.
Not without some funky coding magic at least.
Yes I have tried this inorder to place the manadory options first and then shift by 2 But I get the following error
-2: shift count out of range
Any ideas what i am doing wrong !
while getopts b:t:p opt ; do
shift $(( OPTIND-2 ))
1) As catkin asked above, please use [code][/code] tags around your code, to preserve formatting and to improve readability.
2) As I said, you have to reverse the process. Save the first two parameters to variables first, then shift 2 to remove them from the command list, then use getopts on the rest.
Again, this just reverses the traditional pattern, where the getopts options are processed first, and discarded, so that whatever is left can be used in other ways.
Look at the link I gave you and carefully read how getopts works. Look up what shift does too if necessary (hint, there's a link to "handling positional parameters" on that page that explains it, along with other useful info related to this topic).
Or you could just give the "mandatory" arguments their own getopts flags too, then you wouldn't have to worry about the order they come in at all.
how could I achieve this inside the getopts would i need to pass in aoption for the filename etc
eg -f ?
" you could just give the "mandatory" arguments their own getopts flags too, then you wouldn't have to worry about the order they come in at all. "
Yes, just give them their own flags.
What makes an option "mandatory" or "optional" is not whether it has a getopts flag, it's all in what tests you run when and after you process the parameter. All getopts does is provide an easy way to have a flag-style input.
So, just add a "f:" to flags list, and use that to set the filename variable inside the getopts loop. Then after finishing getopts, run a test on that variable to see if the file exists. If it doesn't, throw up an error.
|All times are GMT -5. The time now is 06:46 PM.|