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.
精彩评论