在C++中,你可以使用system()
函数来执行Shell脚本
#include#include int main() { // 要执行的Shell脚本文件路径 std::string scriptPath = "/path/to/your/script.sh"; // 使用system()函数执行Shell脚本 int result = system(scriptPath.c_str()); // 检查命令执行结果 if (result == 0) { std::cout << "Shell脚本执行成功" << std::endl; } else { std::cerr << "Shell脚本执行失败,返回值: " << result << std::endl; } return 0; }
请确保将scriptPath
变量设置为你要执行的Shell脚本的实际路径。注意,这个示例适用于Unix-like系统(如Linux和macOS)。如果你正在使用Windows系统,你需要将脚本扩展名更改为.bat
或.cmd
,并相应地修改代码。