![]() |
BASH script using getopt to parse optional arguments
Hi
i have the following scenario want to run the following script with manadory and optional argumnets Manadory options are : filename="" port="" optional arguments type -t [A/B] balances -b bal prices -p ./test filename port -t A -b bal my code i have that won't parse the options is This is what i have #!/bin/bash filename="" port="" while getopts b:t:p opt do case $opt in b) BAL="${OPTARG}" if [ "$BAL" == "" ] then print_usage $0 else echo "Balances is " $BAL fi ;; t) TYPE="$OPTARG";; p) PRICES="$OPTARG";; ?) print_usage $0 exit 1;; esac done echo "Balances: "$BAL echo "Type: "$TYPE Any ideas ~ |
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: Code:
#!/bin/bashGuessing what the problem is, getopts is traditional; it must have the options (and option arguments) before the arguments. Try Code:
./test -t A -b bal filename port |
You also have to shift the parameters after processing the optional arguments so you can grab what's left.
Code:
while getopts b:t:p opt ; dohttp://wiki.bash-hackers.org/howto/getopts_tutorial 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 ... done filename="$1" port="$2" 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. Code:
filename="$1"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. |
David ,
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. " Code:
|
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. Code:
|
| All times are GMT -5. The time now is 12:55 AM. |