Running shell script from C [duplicate]
Possible Duplicate:
Running a shell command in a c program
I am running a shell script from C.It is executed using system().How to pass parameters to this script?
system
takes a single string containing the entire command line, so you'd pass
system("/your/shell/script 'argument 1' 'argument 2'");
It is rarely a good idea to use system
, because you'll have to escape all meta-characters yourself – even spaces are a problem, as you can see above. You're looking for one of the exec* functions, for example execv. Its first argument is the name of the program (in your case /bin/sh
or the shell script itself), its second is a NULL-terminated list of argument strings:
char* program = "/your/shell/script";
char* args[3];
args[0] = "argument 1";
args[1] = "argument 2";
args[2] = NULL;
execv(program, args);
system("scriptname arg1 arg2")
精彩评论