Creating linux commands

Discussion in 'General Linux' started by bashcommando, May 31, 2014.

  1. bashcommando

    bashcommando Active Member

    Messages:
    212
    Likes Received:
    43
    Trophy Points:
    28
    I want to be able to make my own linux commands. So I learned a bit of C here and there and my skills got better and better. But now I want to get bash input from the commands. I tried the following:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        printf( system("$1") );
    }
    But it said:
    Code:
    test.c: In function 'main':
    test.c:5: warning: passing arg 1 of 'printf' makes pointer from integer without a cast
    Help?

  2. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,273
    Likes Received:
    431
    Trophy Points:
    83
    Best Answer
    Here is an example:
    test.sh
    Code:
    #!/bin/bash
    
    argumentToPass=$1;
    echo "The argument to pass is";
    echo $argumentToPass;
    
    ./test $argumentToPass
    
    
    
    main.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        int i;
        printf("The following arguments were passed to main(): ");
        for(i=1; i<argc; i++) printf("%s ", argv[i]);
        printf("\n");
        return 0;
    }
    
    Example output:
    Code:
    $ ./test.sh hello_C
    The argument to pass is
    hello_C
    The following arguments were passed to main(): hello_C 
    Last edited: May 31, 2014
    bashcommando likes this.
  3. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,273
    Likes Received:
    431
    Trophy Points:
    83
    But for C that does not work.
    But something like this:
    http://stackoverflow.com/questions/498320/pass-arguments-into-c-program-from-command-line

    you have to pass along the arguments to initialize the parameters of the C program.
    http://publications.gbdirect.co.uk/c_book/chapter10/arguments_to_main.html
    Last edited: May 31, 2014
  4. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,273
    Likes Received:
    431
    Trophy Points:
    83
    Best Answer
    Here is an example:
    test.sh
    Code:
    #!/bin/bash
    
    argumentToPass=$1;
    echo "The argument to pass is";
    echo $argumentToPass;
    
    ./test $argumentToPass
    
    
    
    main.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        int i;
        printf("The following arguments were passed to main(): ");
        for(i=1; i<argc; i++) printf("%s ", argv[i]);
        printf("\n");
        return 0;
    }
    
    Example output:
    Code:
    $ ./test.sh hello_C
    The argument to pass is
    hello_C
    The following arguments were passed to main(): hello_C 
    Last edited: May 31, 2014
    bashcommando likes this.
  5. pane-free

    pane-free Active Member

    Messages:
    357
    Likes Received:
    83
    Trophy Points:
    28
    If PERL used, OP won't have to initialize anything. Try PERL.
    ryanvade likes this.
  6. MikeyD

    MikeyD Active Member

    Messages:
    232
    Likes Received:
    112
    Trophy Points:
    43
    You can do that with shell scripts a bit easier as well.
    What I do is I have a /bin directory in my home dir and just make sure to include that in my $PATH. Any shell scripts I have that are especially helpful you can throw in that /bin dir (rename from "name.sh" to "name" if you're using an extension) and you should be able to access it like any other command.


    In C:

    @ryanvade showed how to do it, but C is an entirely different beast no Linux symbols will carry over. The main function takes two arguments:
    int argc = an integer (argument count) that displays the total number of arguments INCLUDING THE FUNCTION CALL.
    and
    char* argv[] = an argument vector you can think of as a multi-dimensional array it is an array of strings (which are themselves arrays)

    So, for example running:
    "./myprog arg1 arg2"

    argc will be 3
    and
    argv will be {"./myprog", "arg1", "arg2" }

    argv[0] = "./myprog"
    argv[1] = "arg1"
    argv[2] = "arg2"

    Here is an example I used in a C class I'm assisting:


    Code:
    /*
    *
    * Using command-line arguments
    *
    * argc - argument count
    * argv - argument vector
    *
    */
    
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
            printf("%d arguments supplied.\n\n",argc);
    
            // sanity check
            if (argc < 2)
            {
                    printf("Not enough command-line arguments.\n");
            }
            else
            {
                    // iterate over commandline args
                    for (int i = 1;i<argc;i++)
                    {
                            printf("%s\n", argv[i]);
                            int count = strlen(argv[i]);
                            printf("Argument %i has %i characters: ", i, count);
                            for (int j = 0;j < count;j++)
                            {
                                    printf("%c ",argv[i][j]);
                            }
                            printf("\n\n");
                    }
            }
            return 0;
    }
    
    Last edited: Jun 2, 2014
    ryanvade likes this.

Share This Page