Sponsored Content
Top Forums UNIX for Advanced & Expert Users Parsing the command line arguments Post 45277 by jayakhanna on Wednesday 17th of December 2003 10:04:40 AM
Old 12-17-2003
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..
 

10 More Discussions You Might Find Interesting

1. Programming

command line arguments

Hi How to pass multi line text as a command line argument to a program. (i.e) ./a.out hi this is sample 0 file1 where hi this is sample should be stored in argv 0 in argv and so on... (3 Replies)
Discussion started by: bankpro
3 Replies

2. UNIX for Dummies Questions & Answers

arguments in command line

Hi all, How many arguments can we pass while testing a prgm at command line.. I encountered an issue while passing 10 arguments. For $10 its taking argument passed for $1 followed by 'zero'. can we pass more than 9 arguments /Is there any other way. Thanks, rrs (6 Replies)
Discussion started by: rrs
6 Replies

3. Shell Programming and Scripting

command line arguments

-------------------------------------------------------------------------------- I have this while loop and at the end I am trying to get it to tell me the last argument I entered. And with it like this all I get is the sentence with no value for $1. Now I tried moving done after the sentence... (1 Reply)
Discussion started by: skooly5
1 Replies

4. UNIX for Dummies Questions & Answers

Command line arguments.

I am working on a script wherein i need the user to enter the Build ID for eg:the command line will show enter the build ID Now on entering the build ID it should be assigned to @ARGV. How can this be done.? (1 Reply)
Discussion started by: Varghese
1 Replies

5. Shell Programming and Scripting

Help parsing command line arguments in bash

Looking for a little help parsing some command line arguments in a bash script I am working on, this is probably fairly basic to most, but I do not have much experience with it. At the command line, when the script is run, I need to make sure the argument passed is a file, it exists in the... (3 Replies)
Discussion started by: Breakology
3 Replies

6. Shell Programming and Scripting

Maximum command line arguments

Hi, Can anyone please help me to know what is the maximum number of command line arguments that we can pass in unix shell script? Thanks in advance, Punitha.S (2 Replies)
Discussion started by: puni
2 Replies

7. UNIX for Dummies Questions & Answers

command line arguments

hi, can someone how to accept command line arguments as a variable using in script? like: ./scriptname arguments by accept arguments, I can use it in my script? thx! (1 Reply)
Discussion started by: ikeQ
1 Replies

8. Shell Programming and Scripting

command line arguments

hi,,,, I want to create a command prompt, for example "prompt>", so my prompt need to handle commands, for example "prompt>cmd", so i want to know how to get arguments for my own commands cmd, i.e. default argc should contain arguments count and argv should point to the argument vector i.e, for... (2 Replies)
Discussion started by: vins_89
2 Replies

9. Programming

Parsing command line arguments in Python

Hi, I've a python script called aaa.py and passing an command line option " -a" to the script like, ./aaa.py -a & Inside the script if the -a option is given I do some operation if not something else. code looks like ./aaa.py -a . . if options.a ---some operation--- if not options.a... (1 Reply)
Discussion started by: testin
1 Replies

10. Shell Programming and Scripting

Parsing Command Line Arguments In C shell script

]I have a string like "/abc/cmind/def/pq/IC.2.4.6_main.64b/lnx86" and this string is given by user. But in this string instead of 64b user may passed 32 b an i need to parse this string and check wether its is 32b or 64 b and according to it i want to set appropriate flags. How will i do this... (11 Replies)
Discussion started by: codecatcher
11 Replies
GETOPT(3)						     Library Functions Manual							 GETOPT(3)

NAME
getopt - get option character from command line argument list SYNOPSIS
#include <stdlib.h> extern char *optarg; extern int optind; extern int optopt; extern int opterr; extern int optreset; int getopt(argc, argv, optstring) int argc; char **argv; char *optstring; DESCRIPTION
The getopt() function incrementally parses a command line argument list argv and returns the next known option character. An option char- acter is known if it has been specified in the string of accepted option characters, optstring. The option string optstring may contain the following elements: individual characters, and characters followed by a colon to indicate an option argument is to follow. For example, an option string """x"" recognizes an option ``-x'', and an option string """x:"" recognizes an option and argument ``-x argument''. It does not matter to getopt() if a following argument has leading white space. On return from getopt(), optarg points to an option argument, if it is anticipated, and the variable optind contains the index to the next argv argument for a subsequent call to getopt(). The variable optopt saves the last known option character returned by getopt(). The variable opterr and optind are both initialized to 1. The optind variable may be set to another value before a set of calls to getopt() in order to skip over more or less argv entries. In order to use getopt() to evaluate multiple sets of arguments, or to evaluate a single set of arguments multiple times, the variable optreset must be set to 1 before the second and each additional set of calls to getopt(), and the variable optind must be reinitialized. The getopt() function returns an EOF when the argument list is exhausted, or a non-recognized option is encountered. The interpretation of options in the argument list may be cancelled by the option `--' (double dash) which causes getopt() to signal the end of argument process- ing and return an EOF. When all options have been processed (i.e., up to the first non-option argument), getopt() returns EOF. DIAGNOSTICS
If the getopt() function encounters a character not found in the string optarg or detects a missing option argument it writes an error mes- sage and returns `?' to the stderr. Setting opterr to a zero will disable these error messages. If optstring has a leading `:' then a missing option argument causes a `:' to be returned in addition to suppressing any error messages. Option arguments are allowed to begin with `-'; this is reasonable but reduces the amount of error checking possible. EXTENSIONS
The optreset variable was added to make it possible to call the getopt() function multiple times. This is an extension to the IEEE Std1003.2 (``POSIX'') specification. EXAMPLE
extern char *optarg; extern int optind; int bflag, ch, fd; bflag = 0; while ((ch = getopt(argc, argv, "bf:")) != EOF) switch(ch) { case 'b': bflag = 1; break; case 'f': if ((fd = open(optarg, O_RDONLY, 0)) < 0) { (void)fprintf(stderr, "myname: %s: %s ", optarg, strerror(errno)); exit(1); } break; case '?': default: usage(); } argc -= optind; argv += optind; HISTORY
The getopt() function appeared 4.3BSD. BUGS
A single dash ``-'' may be specified as an character in optstring , however it should never have an argument associated with it. This allows getopt() to be used with programs that expect ``-'' as an option flag. This practice is wrong, and should not be used in any cur- rent development. It is provided for backward compatibility only . By default, a single dash causes getopt() to return EOF. This is, we believe, compatible with System V. It is also possible to handle digits as option letters. This allows getopt() to be used with programs that expect a number (``-3'') as an option. This practice is wrong, and should not be used in any current development. It is provided for backward compatibility only. The following code fragment works in most cases. int length; char *p; while ((c = getopt(argc, argv, "0123456789")) != EOF) switch (c) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': p = argv[optind - 1]; if (p[0] == '-' && p[1] == ch && !p[2]) length = atoi(++p); else length = atoi(argv[optind] + 1); break; } } 4.3 Berkeley Distribution January 12, 1996 GETOPT(3)
All times are GMT -4. The time now is 01:58 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy