Skip to content
Advertisement

How to pass run time arguments to a function in c through a shell script

I have a shell script which has to take arguments from the command line and pass it to a function in C. I tried to search but didn’t find understandable solutions. Kindly help me out. Should the arguments be passed via an option as a command in the shell script?

I have a main function like this:

int main(int argc, char *argv[])
{
    if(argc>1)
    {
        if(!strcmp(argv[1], "ABC"))
        {



        }
        else if(!strcmp(argv[1], "XYZ"))   
        {

        }
    }
}

How to pass the parameters ABC/XYZ from the command line through a shell script which in turn uses a makefile to compile the code?

Advertisement

Answer

You cannot meaningfully compare strings with == which is a pointer equality test. You could use strcmp as something like argc>1 && !strcmp(argv[1], "XYZ"). The arguments of main have certain properties, see here.

BTW, main‘s argc is at least 1. So your test argc==0 is never true. Generally argv[0] is the program name.

However, if you use GNU glibc (e.g. on Linux), it provides several ways for parsing program arguments.

There are conventions and habits regarding program arguments, and you’ll better follow them. POSIX specifies getopt(3), but on GNU systems, getopt_long is even more handy.

Be also aware that globbing is done by the shell on Unix-like systems. See glob(7).

(On Windows, things are different, and the command line might be parsed by some startup routine à la crt0)

In practice, you’ll better use some system functions for parsing program arguments. Some libraries provide a way for that, e.g. GTK has gtk_init_with_args. Otherwise, if you have it, use getopt_long

Look also, for inspiration, into the source code of some free software program. You’ll find many of them on github or elsewhere.

How to pass the parameters ABC/XYZ from the command line through a shell script

If you compile your C++ program into an executable, e.g. /some/path/to/yourexecutable, you just have to run a command like

 /some/path/to/yourexecutable ABC

and if the directory /some/path/to/ containing yourexecutable is in your PATH variable, you can simply run yourexecutable ABC. How to set that PATH variable (which you can query using echo $PATH in your Unix shell) is a different question (you could edit some shell startup file, perhaps your $HOME/.bashrc, with a source code editor such as GNU emacs, vim, gedit, etc…; you could run some export PATH=…. command with an appropriate, colon-separated, sequence of directories).

which in turn uses a makefile to compile the code?

Then you should look into that Makefile and you’ll know what is the executable file.


You are using and coding on/for Linux, so you should read something about Linux programming (e.g. ALP or something newer; see also intro(2) & syscalls(2)…) and you need to understand more about operating systems (so read Operating Systems: Three Easy Pieces).

Advertisement