脚本参考之前的文章,
代码如下:
#includeusing namespace std; static inline std::string execute_command(const char *cmd) { if (cmd == NULL) { return ""; } FILE *fp = popen(cmd, "r"); if (NULL == fp) { return ""; } char buf[BUFSIZ] = {}; int i; std::string result; while ((i = fread(buf, sizeof(char), BUFSIZ, fp)) > 0) { result.append(buf, i); memset(buf, 0, BUFSIZ); if (i != BUFSIZ) { break; } } pclose(fp); int index = result.find_last_of("rn"); if (index > 0) { return result.substr(0, index); } index = result.find_last_of("n"); if (index > 0) { return result.substr(0, index); } return result; } int main() { string cmd = R"(bash proc_ex.sh)"; string ret = execute_command(cmd.c_str()); cout << ret << endl; return 0; }



