![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Getting error in command line arguments | sunitachoudhury | Shell Programming and Scripting | 9 | 01-14-2009 09:13 PM |
| command line arguments | skooly5 | Shell Programming and Scripting | 1 | 04-07-2008 03:49 AM |
| arguments in command line | rrs | UNIX for Dummies Questions & Answers | 6 | 07-28-2006 06:44 AM |
| command line arguments | bankpro | High Level Programming | 3 | 02-02-2006 11:12 AM |
| Command Line Arguments | roba909 | UNIX for Dummies Questions & Answers | 7 | 01-19-2006 01:46 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Parsing the command line arguments
Is there a way to get the command line arguments.
I am using getopt(3) but if the arguments are more than one for a particular option than it just ignores the second argument. For eg ./a.out -x abc def now abd will be got with -x using getopt "( x : )" and string abc\0def will get stored in optarg. Now while parsing I get the \0 in between so how can I get this sorted out. Basically I want to enter the command like this ./a.out -k abc def -a junk -b junk1 now I want to store "abc" and "def" in some string. And parse further the options with -a and -b I am using solaris 5.8 Regards JK Last edited by jayakhanna; 12-17-2003 at 08:31 AM.. |
|
||||
|
So you want 'abc' and 'def' in the same string, why don't you simply put a quote around it, i.e.
./a.out -k 'abc def' -a junk -b junk2 then getopt(argc, argv, "k:a ![]() Last edited by cbkihong; 12-17-2003 at 10:00 AM.. |
|
||||
|
Yeah that is a workaround that will do,
But I am really wondering how to do the following suppose I am using getopt function, in a C program and try to caputre the arguments with the -x option then how can I do that. Like I have a command ls abc def, this is just an example now I want to wrap it up in some other function. So basically i write this Code:
main()
{
char ch;
char buf[100];
while ( (ch = getopt (x:a:b:)) != NULL) {
switch (ch) {
case 'x' :
strcpy (buf, optarg);
I tried to do this Code:
for ( i = 0; i < 50; i++) {
dest[i ] = optarg[i ];
}
Here I am giving the i value randomly, how will I determine the exact i value. Now this arguments needs to be passed to ls which i will call in some other part like system ("ls arguments) got from above which will stored in some variable Hoping that you understood the real problem Regards JK [i]added code tags and spaces to so rest of post wasn't italicized --oombera Last edited by oombera; 02-21-2004 at 02:32 AM.. |
|
||||
|
Yes, I understand your problem. I don't know if any Unix variants have special extensions. Otherwise, from my interpretation of the getopt manpage I believe getopt doesn't expect multiple parameters to a single option, at all.
So, for example, if you invoke this as ./a.out -x abc def -a Then argv[] is an array consisting of -x abc def -a (not showing argv[0] above) What getopt does is that it monotonically scans forward. It identifies '-x', associates 'abc' as optarg. It doesn't know what 'def' is, and throw it to the end. Then it identifies '-a', and so on. 'def' is left at the end and regarded as a non-option (the index of which you get with optind). Therefore, as getopt proceeds it permutes the order of arguments that appear in argv[]. I made this test program to illustrate the idea: Code:
#include <stdio.h>
#include <getopt.h>
extern char *optarg;
extern int optind;
int main(int argc, char** argv) {
int optchr; int i;
while ((optchr = getopt(argc, argv, "x:a")) != -1) {
printf("Option '%c' detected.\n", optchr);
if (optarg) {
printf("\tArgument: '%s'\n", optarg);
}
}
printf("Other arguments:\n");
for(i=optind; i<argc; i++) {
printf("\t%s\n", argv[i]);
}
return 0;
}
gives Code:
Option 'x' detected.
Argument: 'abc'
Option 'a' detected.
Other arguments:
def
|
![]() |
| Bookmarks |
| Tags |
| solaris |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|