You could check for arguments before setting flags...
Additionly, it gets difficult if your options require 2 arguments as shown in:
AFAIK: getopts can only handle options before the actual arguments.
NOTE: There is a getopt and a getopts, not sure if either (or which) one of them is for ksh...
Specialy, NONE of your options catch arguments you want to pass...
In some occasions, it even makes sense to force the user to enter variables in a certain order, with the last one beeing optional.
Or that options just trigger the handler (but have a preset value).
Furthermore, using getopts provides you with $OPTARG, which represents (somewhat) $1.
Once you 'passed' the getopts part, and the according shift $(($OPTIND-1)), the remaining ARGUMENTS, since all options and their arguments, have been removed by the previous shift command.
Saying, in your current flag check blocks, you are refering to empty (since removed) variables, thus the script fails.
You need to set all required argument of your options inside the getopts block.
In the like of (cut-out snippet from one of my scripts):
Hope this helps
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,288
Thanks Given: 430
Thanked 480 Times in 395 Posts
Hi.
I have not tried to use multiple, whitespace-sparated arguments to options, so I would try first setting up your code to assume either a quoted string or a comma-separated string:
or
Of course, you will need to do the work of splitting the string into the correct variables.
The version of getotps in at least ksh 93u+ allows complex forms, so perhaps it can accept arguments such as you desire. See getopts --man
I think there are other codes which may process multiple strings such as you have intended to use, but I am fairly sure that the kshgetopts is not one of them.
Appendix B of Learning the Korn Shell, 2nd, has many more details on getopts.
I seem to recall seeing simple shell code to collect strings until they hit an argument that began with with a [+-] -- so you could roll your own.
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,288
Thanks Given: 430
Thanked 480 Times in 395 Posts
Hi.
Quote:
Originally Posted by drl
I seem to recall seeing simple shell code to collect strings until they hit an argument that began with with a [+-] -- so you could roll your own.
Apparently this looked familiar to me because I had written one.
This script, s5, will show 3 methods for accumulating multiple arguments:
This is a driver script, run5, that will exercise the methods:
Executing run5 produces:
Best wishes ... cheers, drl
Sometimes, manual interaction is simpler than using builtins...
In this example, we parse the arguments as they are occouring, and remove them as soon they are read.
(untested but should work)
What you would need to do, is to check (specialy for -c) if the 'following' arguments, do start with a "-" or otherwise not match.
Remind you, since the -b or -c was already removed, you can simply check (for example) if there are enough (3) follow up arguments by [[ 3 -eq $# ]] inside the case block.
Hope this helps
Last edited by sea; 12-09-2014 at 09:28 PM..
Reason: Code fix
Hi veeresh_15,
The "complete" script you provided shows us why you are having problems using getopts, but it doesn't explain what you're really trying to do. You have shown us four sample command lines used to test your option handling capabilities, but to understand how to best help you, what we really need is the Synopsis lines from the man page for your utility (or better yet, the complete man page) so we can understand what options (with and without option arguments) and what operands your script will need to process for your utility.
It is unusual to need an option for "defaults". If you're setting up a utility to create databases, what you're describing as defaults (the database ID, version, and type) don't seem like defaults; they seem like something that would be required to set up any database and they would be different for each database you create. If that is the case here, these three items should be mandatory operands; not options.
I can easily see having default values for database block size and character sets that would apply to most databases you create. And, having options (with option arguments) to override those defaults is perfectly reasonable.
So, if I have guessed correctly, your Synopsis would be something like:
and your code should be something more like:
And, then you could successfully invoke it with command lines like:
PS
If you're using a 1993 or later version of ksh, you'll get bad option diagnostics consistent with the rest of the diagnostics produced by the above script if you change:
to:
Last edited by Don Cragun; 12-09-2014 at 06:36 PM..
Reason: Add PS.
This User Gave Thanks to Don Cragun For This Post:
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)
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)
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)
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)
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)
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)
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)
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)