C++调用shell脚本

调用函数时候,传入脚本路径名称或者具体命令。

int shell_call(std::string &cmdstr) {
  enum { maxline=100 };
  char line[maxline];
  FILE *fpin;
  int ret;
  if((fpin = popen(cmdstr.c_str(), "r")) == NULL) {
    printf("popen error\n");
    exit(-1);
  }
  for(;;) {
    fputs("prompt> ", stdout);
    fflush(stdout);
    if(fgets(line, maxline, fpin) == NULL) /*read from pipe*/
      break;
    if(fputs(line, stdout) == EOF) {
      printf("fputs error\n");
      exit(-1);
    }
  }
  if((ret = pclose(fpin)) == -1) {
    printf("pclose error\n");
    exit(-1);
  }
  return ret;
}

原文链接: https://www.cnblogs.com/donggongdechen/p/8949440.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/272938

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 下午11:10
下一篇 2023年2月14日 下午11:11

相关推荐