Using getopts for handling multiple options


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using getopts for handling multiple options
# 8  
Old 12-10-2014
Hi Don Cragun,

Apologies for the ambiguity in my question, you have correctly understood my requirement. My idea of having -d (default) was to have those three arguments mandatory.

Your sample code really serves my purpose, thanks a lot for your time and guidance!

Regards,
Veeresham

---------- Post updated at 03:48 PM ---------- Previous update was at 01:56 PM ----------

Hi Don,

Script works fine as long as i provide -b & -c and their arguments before the mandatory arguments.
Code:
createdb -b 20480 -c"UTF-8" MyDatabase 1 MyType

But if i execute the script this way:

Code:
createdb MyDatabase 1 MyType -b 20480 -c"UTF-8"

Its not taking the values specified with -b or -c. Instead its taking the default values defined in the script.

Regards,
Veeresham
# 9  
Old 12-10-2014
Quote:
Originally Posted by veeresh_15
Hi Don,

Script works fine as long as i provide -b & -c and their arguments before the mandatory arguments.
Code:
createdb -b 20480 -c"UTF-8" MyDatabase 1 MyType

But if i execute the script this way:

Code:
createdb MyDatabase 1 MyType -b 20480 -c"UTF-8"

Its not taking the values specified with -b or -c. Instead its taking the default values defined in the script.

Regards,
Veeresham
Yes. The standard Utility Syntax Guidelines say that options should come before operands on the command line. The getopts utility assumes that your command lines follow this guideline.

Instead of taking the default values, the script I gave you should have generated a diagnostic something like:
Code:
createdb: 3 operands are required, 6 found.
Usage: created [-b block_size] [-c character_set] sid version type

I know that some systems "reorder" arguments passed to some utilities to put what it believes are options before what it believes are operands. The standards don't allow this; options are required to be presented before operands except in a very few cases (like compilers) where options may need to vary for different operands. I don't see the need for the extra complexity for the script you have described in this thread.

You can code around this restriction, but it makes option and operand parsing MUCH more difficult. I won't offer to modify the script I suggested earlier to allow options to be mixed in with operands.

Sorry,
Don
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SFTP multiple options

Hi, I am trying to SFTP files in a script that i created. But the problem is i have to use -oPort and -b together. how can i get this done. I have tried as below command in my script but with no luck sftp -oPort=102 -b <batchfilename> username@server sftp -oPort=102 -ob... (1 Reply)
Discussion started by: ramkiran77
1 Replies

2. Shell Programming and Scripting

ISSUE in handling multiple same name files :-(

Dear all, My work is completely stuck cos of the following issue. Please find it here and kindly help me. Task is following: I have set of files with such pattern 1t-rw-rw-r-- 1 emily emily 119 Jun 11 10:45 vgtree_5_1_pfs.root 3t-rw-rw-r-- 1 emily emily 145 Jun 11 10:46 vgtree_5_3_pfs.root... (4 Replies)
Discussion started by: emily
4 Replies

3. Shell Programming and Scripting

Multiple variables options

Hi I'm looking to take a user input and use it to effect just two characters in a command rather than having multiple functions for each one. function baseencode() { echo "This function handles the following: $YELLOW base64 base32 base16 $NORMAL" echo "$GREEN Select 64 32 or 16 $NORMAL"... (2 Replies)
Discussion started by: 3therk1ll
2 Replies

4. Shell Programming and Scripting

Help with Handling multiple argument in shell script

Hi i have written a shell script that takes only single ip address from the user and calculates its latency and reliability, can you please tell me that what should be done if i want that user should enter 100 or 1000 ip address (5 Replies)
Discussion started by: Preeti_17
5 Replies

5. Shell Programming and Scripting

Intersperse arguments and options w/ getopts

Is it possible to get a script that uses getopts to accept options and arguments in any order? eg. -g -h 2 4 works like -g 2 -h 4. (1 Reply)
Discussion started by: lee.n.doan
1 Replies

6. Programming

Handling Multiple terminals

Hi, Basically I've written a game in ncurses that supports multiple players. Each player has a process associated with him which shares a segment of memory in which the player's structures are stored, and these structured are accessed by the 'server' program and handled there. The scope of the... (13 Replies)
Discussion started by: dgre0018
13 Replies

7. UNIX for Advanced & Expert Users

shred multiple options

I've created a wxpython gui for the shred command. I can successfully mix and match all the shred options except two: -size and --random-source. (Man page definitions below). -size and --random-source seem to only work when they are used as the sole option passed. For example, I can zero a... (0 Replies)
Discussion started by: codecellar
0 Replies

8. Shell Programming and Scripting

File handling, getopts command in unix

I need to create a shell script having the menu with few options such as 1. Listing 2. Change permissions 3. Modify Contents 4. Delete Files 5. Exit 1. For 1. Listing: Display a special listing of files showing their date of modification and access time (side by side) along with their... (2 Replies)
Discussion started by: bab123
2 Replies

9. UNIX for Advanced & Expert Users

Multiple file handling

Dear All, I have two files, which looks like: File 1 124 235 152 178 156 142 178 163 159 File 2 124|5623 452|6698 178|9995 (8 Replies)
Discussion started by: rochitsharma
8 Replies

10. Shell Programming and Scripting

getopts takes options for parameters

Here is my post with a question about getopts. I am running korn shell on Solaris 5.8. I am trying to ensure that certain options require a parameter, which is easy enough. I have found that if multiple options are entered on the command line together, but the parameter for one of the options is... (1 Reply)
Discussion started by: UCD-Randy
1 Replies
Login or Register to Ask a Question