I'm learning how to add in programm another option by command line.
For example in the "my_prog" i want to add a " -k " option and then write a number.
I mean:
and the i should use the number 50.
I'm reading about getopt_long and optarg, and what i have done in the code is to add k in the getop_long as:
and the k option in the switch as:
the program run but it doesn't work appropriately. I think the problem is in optarg that should point to a string.
Anyone can help?
Thanks
D.
-----Post Update-----
Sorry I got it.
All i have to do is translate the char to int with the atoi() function.
Now seems like works!
Hello Friends,
I need to pass arguments to a shell scripts. but for this i need to take the arguments only if they are supplied to the script as arguments.
Like :
Prompt > scriptname -d device_cd -s message
so how do i capture these arguments ?> i think we have to use optarg.but i dont... (5 Replies)
Hi I have below code in one of my shell script:
if ; then fail $USAGE; fi
while getopts hz:r:t:dz: o
do
case "$o" in
h) echo $USAGE ; exit 0;;
r) export REQ_ID="$OPTARG";;
t) TIMESPAN="$OPTARG";;
d) detail="true";;
) ... (0 Replies)
Hi there,
I hope, that I do not open an thread, that is already existing, but I didn't found something matching with my problem while searching for problems with "getopts"
My problem ist, that I'm taking arguments from commandline into my script with getopts, I've an flag -s, after that there... (2 Replies)
I want to add a "-r <remote_host>" option to my ksh script, causing the script to run a script of the same name on the specified remote host. The remote invocation should itself include all the command-line options of the original invocation, less the -r option.
For example, this invocation:
... (7 Replies)
i'm confused what this means.
i was asked to design a menu or command line option driven script that reads out of a DB and displays info such as
read_data.pl -u <user> -e <event>
which would print commands run by <user>with the <event> in the db.
any suggestions? i've been using... (2 Replies)
could someone show me a sample command line option driven script?
i want to see an easy way to write one and how i can execute it using command line options such as typing in
read.pl -i <id> -c <cmds> -s <start> -e <end>
would read out all the commands run by ID . from start time to... (7 Replies)
Hi,
I am creating a program with the C language that simulates the WC command in Unix. My program needs to count lines, bytes and words. I have not added the code to count bytes and words yet. I am having trouble understanding what the file option/flag '-' does. I can not visualize how it moves... (1 Reply)
I have a C++ program. I read command line arguments, but if the value is not supplied, I default or make a calculation. Let's say I set it to a default value.
I can code this in several ways. Here I show three ways. What would be the best way for maintaining this code? The program will get very... (2 Replies)
Hi all!
I have a data set in this tab separated format : Label, Value1, Value2
An instance is "data.txt" :
0 1 1
-1 2 3
0 2 2
I would like to parse this data set and generate two files, one that has only data with the label 0 and the other with label -1, so my outputs should be, for... (1 Reply)
I am looking for help in processing of those options: '-n' or '-p'
I understand what they do and how to use them.
But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.)
I did try it and -n works on 2 files.
Question is:
- is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies
LEARN ABOUT MINIX
getopt
GETOPT(3) Library Functions Manual GETOPT(3)NAME
getopt - get option letter from argv
SYNOPSIS
int getopt(argc, argv, optstring)
int argc;
char **argv;
char *optstring;
extern char *optarg;
extern int optind;
DESCRIPTION
Getopt returns the next option letter in argv that matches a letter in optstring. Optstring is a string of recognized option letters; if a
letter is followed by a colon, the option is expected to have an argument that may or may not be separated from it by white space. Optarg
is set to point to the start of the option argument on return from getopt.
Getopt places in optind the argv index of the next argument to be processed. Because optind is external, it is normally initialized to
zero automatically before the first call to getopt.
When all options have been processed (i.e., up to the first non-option argument), getopt returns EOF. The special option -- may be used to
delimit the end of the options; EOF will be returned, and -- will be skipped.
DIAGNOSTICS
Getopt prints an error message on stderr and returns a question mark (?) when it encounters an option letter not included in optstring.
EXAMPLE
The following code fragment shows how one might process the arguments for a command that can take the mutually exclusive options a and b,
and the options f and o, both of which require arguments:
main(argc, argv)
int argc;
char **argv;
{
int c;
extern int optind;
extern char *optarg;
.
.
.
while ((c = getopt(argc, argv, "abf:o:")) != EOF)
switch (c) {
case `a':
if (bflg)
errflg++;
else
aflg++;
break;
case `b':
if (aflg)
errflg++;
else
bproc();
break;
case `f':
ifile = optarg;
break;
case `o':
ofile = optarg;
break;
case `?':
default:
errflg++;
break;
}
if (errflg) {
fprintf(stderr, "Usage: ...");
exit(2);
}
for (; optind < argc; optind++) {
.
.
.
}
.
.
.
}
HISTORY
Written by Henry Spencer, working from a Bell Labs manual page. Modified by Keith Bostic to behave more like the System V version.
BUGS
It is not obvious how `-' standing alone should be treated; this version treats it as a non-option argument, which is not always right.
Option arguments are allowed to begin with `-'; this is reasonable but reduces the amount of error checking possible.
Getopt is quite flexible but the obvious price must be paid: there is much it could do that it doesn't, like checking mutually exclusive
options, checking type of option arguments, etc.
4.3 Berkeley Distribution May 27, 1986 GETOPT(3)