以下代码仅实现在 C 语言程序中运行 shell 脚本:
#include
#include
int main() {
// 使用 system 函数调用 shell 脚本
int result = system("sh your_script.sh");
// 检查执行结果
if (result == -1) {
perror("Error executing the shell script");
return EXIT_FAILURE;
} else {
printf("Shell script executed successfully\n");
return EXIT_SUCCESS;
}
}
也在网上搜索到很多在 C 语言中逐行写入 shell 脚本命令的方法,觉得不太方便。请教大佬们怎么把一个已经编写好的 shell 脚本文件“内置”或者说“隐藏”到程序中,让人看起来就是一个可执行程序,每次运行程序时,就会“释放”这个 shell 脚本到/tmp 目录下,再执行 sh /tmp/myshell.sh 。