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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM