The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-17-2003
jayakhanna jayakhanna is offline
Registered User
  
 

Join Date: Dec 2003
Location: India
Posts: 50
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..
  #2 (permalink)  
Old 12-17-2003
oombera's Avatar
oombera oombera is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2002
Location: Cleveland, OH
Posts: 804
Use $1 to $9 to get up to 9 parameters... in your case, "-k" would be stored in $1, "abc" would be stored in $2, etc...
  #3 (permalink)  
Old 12-17-2003
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
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") will work ....


Last edited by cbkihong; 12-17-2003 at 10:00 AM..
  #4 (permalink)  
Old 12-17-2003
jayakhanna jayakhanna is offline
Registered User
  
 

Join Date: Dec 2003
Location: India
Posts: 50
I wan' t a system call and need not want to tokenize either, I just want to execute the way as normal program does.

./a.out -k abc def -x adf ghi

Regards
JK
  #5 (permalink)  
Old 12-17-2003
oombera's Avatar
oombera oombera is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2002
Location: Cleveland, OH
Posts: 804
My earlier post wasn't what you wanted.. I don't think I understood your question at first.
Quote:
Originally posted by jayakhanna
I wan' t a system call and need not want to tokenize either, I just want to execute the way as normal program does.
A "normal" program executes the way cbkihong suggested. For example, if you use the find command to find a file called "some file" with a space in it, then you have to surround the name of that file with quotes or the command will fail. From the timestamp on the posts, I can see you most likely didn't see cbkihong's post before you posted...
  #6 (permalink)  
Old 12-17-2003
jayakhanna jayakhanna is offline
Registered User
  
 

Join Date: Dec 2003
Location: India
Posts: 50
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);
now here comes the problem how will I copy optarg to buf, because \0 will be there at the end of the first string so only the first string will be stored in buf

I tried to do this
Code:
                  for ( i = 0; i < 50; i++) {
                          dest[i ] = optarg[i ];
                  }
and then print dest[i ] its showing that optarg has all the arguments whatever we pass with -x, now how to determine how many arguments are passed with -x,

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..
  #7 (permalink)  
Old 12-17-2003
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
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;
}
./a.out -x abc def -a

gives

Code:
Option 'x' detected.
        Argument: 'abc'
Option 'a' detected.
Other arguments:
        def
I think conventional Unix tradition is that there is at most one argument for each option. I think this is because getopt allows options to be given in any order. So if you would like to break this convention you can always write your own routine to parse argv[]. Of course that will not be easy if your program has to accommodate a variety of options. Formally speaking, that requires parser construction knowledge (lexical analysis) that are beyond the scope of this forum.
Closed Thread

Bookmarks

Tags
solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:47 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0