tags:

views:

260

answers:

7

Please explain the syntax of: system(const char *command);

I want to use this function for running the command on unix sytem. I need to execute(automate) several test cases with the same command but,they also have other input values which are different.how do I reuse this code for all the test-cases.

+2  A: 

I don't see how the syntax can be a problem:

system( "foo" );

executes the program called foo, via your preferred shell.

anon
+1  A: 

Generate a command line for each invokation, then pass those command lines into system() one a time.

sharptooth
+2  A: 
int main()
{
    char *base = "./your_testcase " ;
    char aux[50] = "./your_testcase " ;
    char *args[] = {"arg1" ,"arg2" ,"arg3"};
    int nargs = 3;

    for(i=0;i < nargs;i++)
    {
     /* Add arg to the end of the command */
     strcat(aux,args[i]) ;
     /* Call command with parameter */
     system(aux);
     /* Reset aux to just the system call with no parameters */
     strcpy(aux,base);
    }
}
Neeraj
better: snprintf( aux, 50, "%s %s", base, args[ i ]);
William Pursell
A: 

i think Anter is interested in a example:

for instance to remove a file in a directory:

system("/bin/rm -rf /home/ederek/file.txt");

Warrior
can somebody explain why i was given a -1
Warrior
I didn't down vote, but why do you have the recursive option for a file?
Roger Nelson
+2  A: 

Keep in mind that calling system is the same as calling fork and execl. That mean you need to be aware of things like open socket descriptors and file descriptors. I once had a problem with a TCP/IP socket dying on a server because a client was calling system which created a new socket connection to the server that was not being serviced.

Chad Simpkins
A: 

See also the question: 'How to call an external program with parameters?;

http://stackoverflow.com/questions/486087/how-to-call-an-external-program-with-parameters/486409#486409

Roger Nelson
A: 

I would avoid use of the system() function, here is a link to why this might be a bad idea