1

I would like to ask, how to call a shell script with parameters in C. I have found this, but it seems not working.

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

char script = "script.sh";

system(script);

Thanks in advance!

yan
  • 20,644
  • 3
  • 38
  • 48
shaggy
  • 631
  • 4
  • 11
  • 22

3 Answers3

2
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).

Macmade
  • 52,708
  • 13
  • 106
  • 123
1

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.

K.P.
  • 133
  • 2
  • 3
  • 9
0

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.

Brian L
  • 10,757
  • 5
  • 19
  • 17