07-19-2005
Usage of optarg
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 know exact usage.
Can any one please help?
Thanks in advance
Veera
10 More Discussions You Might Find Interesting
1. HP-UX
how can I find cpu usage memory usage swap usage and
I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times
my final destination is monitor process
logical volume usage above X % and number of Logical voluage above
can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies
2. Solaris
Hi,
I want to monitor the current cpu usage, monitor usage , disk I/o and network utlization for solaris using SNMP.
I want the oids for above tasks.
can you please tell me that
Thank you (2 Replies)
Discussion started by: S_venkatesh
2 Replies
3. Programming
Hi
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:
my_prog -k 50
and the i should use the number 50.
I'm reading about getopt_long and optarg, and what i have done in the code... (0 Replies)
Discussion started by: Dedalus
0 Replies
4. AIX
How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies
5. Shell Programming and Scripting
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)
Discussion started by: mohsin.quazi
0 Replies
6. Shell Programming and Scripting
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)
Discussion started by: thommes_pommes
2 Replies
7. Shell Programming and Scripting
here is the code
echo begin
while getopts 1:2:3:4: mode
do
case $mode in
1)echo "You have chosen mode1"
case $OPTARG in
a) echo "User Specified Date Range"
rangelist.sh ;;
b) echo "user specified month and year";;
?) echo "Default, Current... (4 Replies)
Discussion started by: pravsripad
4 Replies
8. UNIX for Dummies Questions & Answers
Hi all,
Can you please tell me the command, with which one can know the amount of space a specific directory has used.
df -k . ---> Displays, the amount of space allocated, and used for a directory.
du -k <dir name> - gives me the memory used of all the files inside <dir>
But i... (2 Replies)
Discussion started by: abhisheksunkari
2 Replies
9. Shell Programming and Scripting
EDIT: -- SOLVED --
Heyas,
Getting used to optargs, but by far not understanding it.
So i have that script that shall be 'changeable', trying to use the passed arguments to modify the script visuals.
Passing:
browser -t test -d sect $HOME
Where -t should change the title,
and -d... (0 Replies)
Discussion started by: sea
0 Replies
10. Shell Programming and Scripting
Hi,
I want to capture space as well from the argument
eg:
script.ksh -m "Message from xyz" -e "email@xyz.com"
script.ksh -m 'Message from xyz' -e 'email@xyz.com'
I am parsing using getopts, but for option "m" OPTARG is returning only "Message".
Please use code tags next time for... (9 Replies)
Discussion started by: tostay2003
9 Replies
LEARN ABOUT ULTRIX
getopt
getopt(3) Library Functions Manual getopt(3)
Name
getopt - get option letter from argument vector
Syntax
#include <stdio.h>
int getopt (argc, argv, optstring)
int argc;
char **argv;
char *optstring;
extern char *optarg;
extern int optind, opterr;
Description
The subroutine returns the next option letter in argv that matches a letter in optstring. The 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. The optarg is set to point to the start of the option argument on return from
The function places in optind the argv index of the next argument to be processed. The external variable optind is automatically initial-
ized to 1 before the first call to
When all options have been processed (that is, up to the first non-option argument), returns EOF. The special option -- may be used to
delimit the end of the options; EOF will be returned, and -- will be skipped.
Diagnostics
The function prints an error message on stderr and returns a question mark (?) when it encounters an option letter that is not included in
optstring. Setting opterr to 0 disables this error message.
Examples
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:
#include <stdio.h>
main (argc, argv)
int argc;
char **argv;
{
int c;
extern int optind, opterr;
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;
bufsiza = 512;
break;
case '?':
errflg++;
}
if (errflg) {
fprintf (stderr, "usage: . . . ");
exit (2);
}
for ( ; optind < argc; optind++) {
if (access (argv[optind], 4)) {
.
.
.
}
See Also
getopt(1)
getopt(3)