C++ call shell script

When calling the function, pass in the script path name or specific command.

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(;;) {
    if(fgets(line, maxline, fpin) == NULL)
      break;
    fputs("prompt> \n", stdout);
    fflush(stdout);
    if(fputs(line, stdout) == EOF) {
      printf("fputs error\n");
      exit(-1);
    }
  }
  if((ret = pclose(fpin)) == -1) {
    printf("pclose error\n");
    exit(-1);
  }

  return ret;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324878403&siteId=291194637
Recommended