开发者

Call a shell script from C

开发者 https://www.devze.com 2023-03-21 07:54 出处:网络
I would like to ask, how to call a shell script with parameters in C. I have found this, but it seems not w开发者_开发知识库orking.

I would like to ask, how to call a shell script with parameters in C. I have found this, but it seems not w开发者_开发知识库orking.

#include <stdio.h>
#include <stdlib.h>

char script = "script.sh";

system(script);

Thanks in advance!


const char * script = "script.sh";

instead of

char script = "script.sh";

Note the «*» sign...

the system function needs a char *, not a single char (a string, not a character).


Basic error: Here you have given a string into the char. That is "char script" can hold only 1 character. For this you need char * script = "script.sh";

Shell Script error: Make sure it is "const char *", also you provide the full path of the script file"script.sh" or whatever command you want to run.

Also you have to add #!/bin/bash on the top after including the libraries.


That's correct if you're passing a valid path to an executable.

http://linux.die.net/man/3/system

You should check the return value and errno for errors.

0

精彩评论

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