+ All Categories
Home > Documents > Anatomy of Command Line Arguments in Linux

Anatomy of Command Line Arguments in Linux

Date post: 07-Aug-2018
Category:
Upload: arkay-naobi
View: 249 times
Download: 0 times
Share this document with a friend

of 22

Transcript
  • 8/21/2019 Anatomy of Command Line Arguments in Linux

    1/22

    The Best Dedicated Serversoftlayer.com/dedicated-server

    Tweet

    7

    reddit

    submit

    7

    LikeLike

    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

  • 8/21/2019 Anatomy of Command Line Arguments in Linux

    2/22

    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

  • 8/21/2019 Anatomy of Command Line Arguments in Linux

    3/22

    #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

  • 8/21/2019 Anatomy of Command Line Arguments in Linux

    4/22

    $./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

  • 8/21/2019 Anatomy of Command Line Arguments in Linux

    5/22

    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

    A f d li i Li h // li b k / d li i li 2/

  • 8/21/2019 Anatomy of Command Line Arguments in Linux

    6/22

    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

    A t f d li t i Li htt // li b k / d li t i li t2/

  • 8/21/2019 Anatomy of Command Line Arguments in Linux

    7/22

    $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

    A t f d li t i Li htt // li b k / d li t i li t2/

  • 8/21/2019 Anatomy of Command Line Arguments in Linux

    8/22

    #include < stdio.h >

    #include

    #include #include

    #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

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

  • 8/21/2019 Anatomy of Command Line Arguments in Linux

    9/22

    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

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

  • 8/21/2019 Anatomy of Command Line Arguments in Linux

    10/22

    #include < stdio.h >

    #include

    #include

    #include

    #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

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

  • 8/21/2019 Anatomy of Command Line Arguments in Linux

    11/22

    /*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

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

  • 8/21/2019 Anatomy of Command Line Arguments in Linux

    12/22

    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

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

  • 8/21/2019 Anatomy of Command Line Arguments in Linux

    13/22

    $ ./mkdevice -c okdev -k

    DEVICE name is okdev

    Option -k requires an argument

    mycmd --display --file file.txt

    ls --all

    #include

    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

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

  • 8/21/2019 Anatomy of Command Line Arguments in Linux

    14/22

    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

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

  • 8/21/2019 Anatomy of Command Line Arguments in Linux

    15/22

    --create : create a device with name as argument

    --number : device should have device number as provided argument.

    #include < stdio.h >

    #include

    #include

    #include

    #include

    #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)

    {

    y g p y g p

    15 of 22 22-Aug-14 10:41 AM

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

  • 8/21/2019 Anatomy of Command Line Arguments in Linux

    16/22

    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

    y g p y g p

    16 of 22 22-Aug-14 10:41 AM

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

  • 8/21/2019 Anatomy of Command Line Arguments in Linux

    17/22

    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

    y g p y g p

    17 of 22 22-Aug-14 10:41 AM

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

  • 8/21/2019 Anatomy of Command Line Arguments in Linux

    18/22

    Share this:

    $ ls -l -t

    $ mkdir --help

    y g p y g p

    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/

  • 8/21/2019 Anatomy of Command Line Arguments in Linux

    19/2219 of 22 22-Aug-14 10:41 AM

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

  • 8/21/2019 Anatomy of Command Line Arguments in Linux

    20/22

    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/

  • 8/21/2019 Anatomy of Command Line Arguments in Linux

    21/2221 of 22 22-Aug-14 10:41 AM

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

  • 8/21/2019 Anatomy of Command Line Arguments in Linux

    22/22


Recommended