Creating linux commands

B

bashcommando

Guest
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?
 


R

ryanvade

Guest
Positional parameters
Arguments passed to the script from the command line [1] : $0, $1, $2, $3 . . .

$0 is the name of the script itself, $1 is the first argument, $2 the second, $3 the third, and so forth. [2] After $9, the arguments must be enclosed in brackets, for example, ${10}, ${11}, ${12}.
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:
R

ryanvade

Guest
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:
M

MikeyD

Guest
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:
$100 Digital Ocean Credit
Get a free VM to test out Linux!


Latest posts

Top