This is really nice. ;)

-Girish

On Mon, Apr 19, 2010 at 6:00 PM, Mohan L <l.mohan...@gmail.com> wrote:
> On Mon, Apr 19, 2010 at 2:49 PM, subhojit ojha <subhojit.o...@gmail.com>wrote:
>
>> Hi all,
>>       I know what is getopts and what is the syntax, how to use it...But I
>> have never came across any shell program where I have been used getopts, is
>> here any one who has implemented getopts in their shell program.....
>> If any body have used, plz share ur experience...Just want to know in which
>> condition we are using getopts.
>>
>>
> Hi,
>
> I don't know how to explain the below usage in shell script. But I will
> explain the usage in C. I have created this very simple  program to show you
> the usage of getopt(). Let us assume that the below program is your
> application(calculation.c). This program will call the function sum(),
> sub(), mul() and divi()  depending upon the option (-S,-s,-m,-d). This is
> the one way parsing command line argument within your application. compile
> and execute the program. You will feel the usage .
>
> //This very basis example will show you the getopt usage
> //calculation.c
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
>
>
> int sum(int , int);
> int sub(int , int);
> int mul(int , int);
> int divi(int , int);
> void usage();
>
> int main(int argc , char *argv[])
> {
>    int c;
>    int input1;
>    /* process arguments */
>    while (-1 != (c = getopt(argc, argv,"S:" "s:" "m:" "d:" "h")))
>    {
>            switch (c)
>        {
>            case 'S':
>                input1=atoi(optarg);
>                printf("Sum : %d\n",sum(input1,2));
>                break;
>            case 's':
>                input1=atoi(optarg);
>                printf("Sub : %d\n",sub(input1,2));
>                break;
>            case 'm':
>                input1=atoi(optarg);
>                printf("Mul : %d\n",mul(input1,2));
>                break;
>            case 'd':
>                input1=atoi(optarg);
>                printf("Div : %d\n",divi(input1,2));
>                break;
>            case 'h':
>                printf("usage goes here\n");
>                usage();
>                break;
>            default:
>                exit(0);
>        }
>    }
>    return 0;
> }
> //addition
> int sum(int x , int y)
> {
>    return (x+y);
>
> }
> //subtraction
> int sub(int x, int y)
> {
>    return (x-y);
> }
> //Multiplication
> int mul(int x, int y)
> {
>    return (x*y);
> }
>
> //devision
> int divi(int x, int y)
> {
>    return (x/y);
> }
>
> void usage()
> {
>    printf("./caculation -S <num1>\twill add: num1+2\n");
>    printf("./caculation -s <num2>\twill sub: num2-2\n");
>    printf("./caculation -m <num3>\twill mul: num3*2\n");
>    printf("./caculation -d <num4>\t will div: num4\2\n");
>    printf("./caculation -h \twill display this help message\n");
> }
>
> Compile :
>
> $ gcc -Wall calculation.c -o claculation
>
> Execute the program with deffent option:
>
> $./claculation -S 10
>
> $./claculation -S 10 -s 20
>
> $./claculation -S 10 -s 20 -m 5
>
> $./claculation -S 10 -s 20 -m 5 -d 30
>
> If you have any questions, please don't hesitate to contact me.
>
> Now take the below link
> http://www.mkssoftware.com/docs/man1/getopt.1.asp
>
> I hope this may help you.
>
> Thanks & Rg
> Mohan L
> _______________________________________________
> ILUGC Mailing List:
> http://www.ae.iitm.ac.in/mailman/listinfo/ilugc
>



-- 
Gayatri Hitech
web: http://gayatri-hitech.com

SpamCheetah Spam filter:
http://spam-cheetah.com
_______________________________________________
ILUGC Mailing List:
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc

Reply via email to