Anda di halaman 1dari 22

The Best Dedicated Server

softlayer.com/dedicated-server
100,000+ Servers Already in Use. Order Yours & Start Work in Seconds
Tweet
7
StumbleUpon
Submit
reddit
submit
7
Like Like
Anatomy of command line arguments in Linux http://mylinuxbook.com/command-line-arguments-in-linux-part2/
1 of 22 22-Aug-14 10:41 AM
int main()
int main(int argc, char *argv[])
int execve(const char *filename, char *const argv[], char *const envp[]);
Anatomy of command line arguments in Linux http://mylinuxbook.com/command-line-arguments-in-linux-part2/
2 of 22 22-Aug-14 10:41 AM
#include < stdio.h >
int main(int arc, char* argv[])
{
int i = 0;
printf(Inside main\n);
for (i = 0; i < argc; i++)
{
printf( %d-th argument received is %s\n, i, argv[i]);
}
return 0;
}
$gcc cmd.c -Wall -o cmd
Anatomy of command line arguments in Linux http://mylinuxbook.com/command-line-arguments-in-linux-part2/
3 of 22 22-Aug-14 10:41 AM
$./cmd --name Rupali --line 12
Inside main
0 argument received is ./cmd
1 argument received is --name
2 argument received is Rupali
3 argument received is --line
4 argument received is 12
th
th
th
th
th
Anatomy of command line arguments in Linux http://mylinuxbook.com/command-line-arguments-in-linux-part2/
4 of 22 22-Aug-14 10:41 AM
getopt_long()
unistd.h
getopt.h
int getopt(int argc, char * const argv[], const char *optstring);
Anatomy of command line arguments in Linux http://mylinuxbook.com/command-line-arguments-in-linux-part2/
5 of 22 22-Aug-14 10:41 AM
Command Option string used in getopt()
ls -lt lt
find -n flilename n:
gcc -c -o output co:
mkdevice [options]
Options:
-c : Create a Device with name as provided
-k : The device node to be created with this device number
Anatomy of command line arguments in Linux http://mylinuxbook.com/command-line-arguments-in-linux-part2/
6 of 22 22-Aug-14 10:41 AM
$mkdevice -c mydevice
$mkdevice -k 3
$mkdevice -c mydevice -k 3
-co:
getopt(argc, argv,"-co:")
compiler main.c -c -o main
Anatomy of command line arguments in Linux http://mylinuxbook.com/command-line-arguments-in-linux-part2/
7 of 22 22-Aug-14 10:41 AM
#include < stdio.h >
#include <string.h >
#include <unistd.h >
#include <stdlib.h >
#define MAXLEN 30
struct Device
{
char name[MAXLEN];
int number;
};
int main(int argc, char** argv)
{
char optc = 0;
struct Device dev = {"dev_0", 112};
int devNum = 0;
while ((optc = getopt(argc, argv,"c:k:")) != -1)
{
switch(optc)
{
case 'c':
printf("Creating the device of name %s\n", optarg);
strncpy(dev.name, optarg, MAXLEN);
break;
case 'k':
devNum = atoi(optarg);
dev.number = devNum;
break;
default:
Anatomy of command line arguments in Linux http://mylinuxbook.com/command-line-arguments-in-linux-part2/
8 of 22 22-Aug-14 10:41 AM
printf("Invalid Option!\n");
exit(0);
}
}
printf("Device %s created !\n", dev.name);
printf("Device : %s\n", dev.name);
printf("Number : %d\n", dev.number);
return 0;
$ gcc mkdevice.c -Wall -o mkdevice
$ ./mkdevice -c mlbdev -k 12
Creating the device of name mlbdev
Device mlbdev created !
Device : mlbdev
Number : 12
while ((optc = getopt(argc, argv,"c:k:")) != -1)
Anatomy of command line arguments in Linux http://mylinuxbook.com/command-line-arguments-in-linux-part2/
9 of 22 22-Aug-14 10:41 AM
#include < stdio.h >
#include <string.h >
#include <unistd.h >
#include <stdlib.h >
#define MAXLEN 30
struct Device
{
char name[MAXLEN];
int number;
};
int main(int argc, char** argv)
{
char optc = 0;
struct Device dev = {"dev_0", 112};
int devNum = 0;
Anatomy of command line arguments in Linux http://mylinuxbook.com/command-line-arguments-in-linux-part2/
10 of 22 22-Aug-14 10:41 AM
/*Just to verify the name of device created before parsing aything.*/
while ((optc = getopt(argc, argv,"c:k:")) != -1)
{
if(optc == 'c')
{
printf("DEVICE name is %s\n", optarg);
if (strlen(optarg) > 10)
{
/*check run-case 1*/
printf("Invalid device name\n");
exit(-1);
}
optind = 1;/*Reseting index to 1 again to start actual parsing*/
break;
}
}
while ((optc = getopt(argc, argv,"c:k:")) != -1)
{
switch(optc)
{
case 'c':
strncpy(dev.name, optarg, MAXLEN);
break;
case 'k':
devNum = atoi(optarg);
dev.number = devNum;
break;
case '?':
if (optopt == 'c' || optopt == 'k')
{
/*check run-case 3*/
printf("Option -%c requires an argument\n", optopt);
exit (-1);
}
Anatomy of command line arguments in Linux http://mylinuxbook.com/command-line-arguments-in-linux-part2/
11 of 22 22-Aug-14 10:41 AM
break;
default:
printf("Invalid Option!\n");
exit(0);
}
}
printf("Device %s created !\n", dev.name);
printf("Device : %s\n", dev.name);
printf("Number : %d\n", dev.number);
return 0;
}
./mkdevice -c ff123456789 -k 5
DEVICE name is ff123456789
Invalid device name
$ ./mkdevice -c okdev -k 15
DEVICE name is okdev
Device okdev created !
Device : okdev
Number : 15
Anatomy of command line arguments in Linux http://mylinuxbook.com/command-line-arguments-in-linux-part2/
12 of 22 22-Aug-14 10:41 AM
$ ./mkdevice -c okdev -k
DEVICE name is okdev
Option -k requires an argument
mycmd --display --file file.txt
ls --all
#include <getopt.h >
int getopt_long(int argc, char * const argv[],
const char *optstring,
Anatomy of command line arguments in Linux http://mylinuxbook.com/command-line-arguments-in-linux-part2/
13 of 22 22-Aug-14 10:41 AM
const struct option *longopts, int *longindex);
struct option
{
const char *name;
/* has_arg can't be an enum because some compilers complain about
type mismatches in all the code that assumes it is an int. */
int has_arg;
int *flag;
int val;
};
Anatomy of command line arguments in Linux http://mylinuxbook.com/command-line-arguments-in-linux-part2/
14 of 22 22-Aug-14 10:41 AM
--create : create a device with name as argument
--number : device should have device number as provided argument.
#include < stdio.h >
#include <string.h >
#include <unistd.h >
#include <stdlib.h >
#include <getopt.h >
#define MAXLEN 30
struct Device
{
char name[MAXLEN];
int number;
};
int main(int argc, char** argv)
{
char optc = 0;
struct Device dev = {"dev_0", 112};
struct option cmdLongOpts[] = {
{"create", required_argument, NULL, 'c'},
{"number", required_argument, NULL, 'k'},
{0, 0, 0, 0}
};
int devNum = 0;
while ((optc = getopt_long(argc, argv,"c:k:", cmdLongOpts, NULL)) != -1)
{
Anatomy of command line arguments in Linux http://mylinuxbook.com/command-line-arguments-in-linux-part2/
15 of 22 22-Aug-14 10:41 AM
switch(optc)
{
case 'c':
printf("Creating the device of name %s\n", optarg);
strncpy(dev.name, optarg, strlen(optarg));
break;
case 'k':
devNum = atoi(optarg);
dev.number = devNum;
break;
default:
printf("Invalid Option!\n");
exit (0);
}
}
printf("Device %s created !\n", dev.name);
printf("Device : %s\n", dev.name);
printf("Number : %d\n", dev.number);
return 0;
}
$ gcc mkdevice.c -Wall -o mkdevice
$ ./mkdevice --create mlbdev
Creating the device of name mlbdev
Device mlbdev created !
Device : mlbdev
Number : 112
$ ./mkdevice --create mlbdev --number 456
Anatomy of command line arguments in Linux http://mylinuxbook.com/command-line-arguments-in-linux-part2/
16 of 22 22-Aug-14 10:41 AM
Creating the device of name mlbdev
Device mlbdev created !
Device : mlbdev
Number : 456
$ ./mkdevice --create mlbdev -k 321
Creating the device of name mlbdev
Device mlbdev created !
Device : mlbdev
Number : 321
optc = getopt_long(argc, argv,"c:k:W;", cmdLongOpts, NULL)
$ ./mkdevice -Wcreate mlbdev -k 321
$ ./mkdevice -Wcreate mlbdev -Wnumber 321
Anatomy of command line arguments in Linux http://mylinuxbook.com/command-line-arguments-in-linux-part2/
17 of 22 22-Aug-14 10:41 AM
Share this:
$ ls -l -t
$ mkdir --help

Anatomy of command line arguments in Linux http://mylinuxbook.com/command-line-arguments-in-linux-part2/


18 of 22 22-Aug-14 10:41 AM
Anatomy of command line arguments in Linux http://mylinuxbook.com/command-line-arguments-in-linux-part2/
19 of 22 22-Aug-14 10:41 AM

Anatomy of command line arguments in Linux http://mylinuxbook.com/command-line-arguments-in-linux-part2/


20 of 22 22-Aug-14 10:41 AM
Anatomy of command line arguments in Linux http://mylinuxbook.com/command-line-arguments-in-linux-part2/
21 of 22 22-Aug-14 10:41 AM
Anatomy of command line arguments in Linux http://mylinuxbook.com/command-line-arguments-in-linux-part2/
22 of 22 22-Aug-14 10:41 AM

Anda mungkin juga menyukai