shaggy
shaggy

Reputation: 651

Call a shell script from C

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!

Upvotes: 1

Views: 2062

Answers (3)

K.P.
K.P.

Reputation: 133

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.

Upvotes: 1

Macmade
Macmade

Reputation: 53960

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

Upvotes: 2

Brian L
Brian L

Reputation: 10955

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.

Upvotes: 0

Related Questions