C 中的 system() 函数

system() function in C

如果我 运行 代码如下,我会执行 shell 脚本 .sh 吗:

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

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

你建议我用什么来执行 C 代码中的 shell 脚本?

使用system 是运行 shell 命令的正确方法。一些注意事项:

  • 您不应该 system 自己申报。相反,做 #include <stdlib.h>
  • 如果担心便携性,请执行 system(NULL)。如果 return 值非零,则处理 system 函数调用的命令处理器可用。
  • 最好使用 shell 脚本的完整路径,或设置路径以执行您要执行的 shell 脚本的版本。