user4929091
user4929091

Reputation:

system() function in C

Will I execute shell script .sh if I run code like this:

int system (char *s);
int sh_exec_ok; //Shell script execution flag

sh_exec_ok = system("something/script_name.sh");

What do you suggest me to use to execute shell scripts in C code?

Upvotes: 2

Views: 424

Answers (1)

Craig S. Anderson
Craig S. Anderson

Reputation: 7374

Using system is the right way to run a shell command. Some notes:

  • You should not declare system yourself. Instead, do #include <stdlib.h>
  • If portability is a concern, do system(NULL). If the return value is non-zero, then a command processor to handle the system function call is available.
  • It is good practice to use the full path to the shell script, or set the path so it executes the version of the shell script you want to execute.

Upvotes: 2

Related Questions