开发者

Running shell script from C [duplicate]

This question already has answers here: 开发者_如何学Go Closed 11 years ago.

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")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜