getting output from excl used to run a shell script

时间:2017-10-12 10:04:49

标签: c linux shell execl

Hi i would like to get suggestions as to getting output from a script which i am supposed to execute from an excl call:

void getCurrentFSLSMode(char* const in_fsls_directory, enum Mode* out_mode) {
  int link[2];
  pid_t pid;

  char buffer[MAX_PATH_STR_LENGTH];
  char command_buffer[MAX_COMMAND_STR_LENGTH];
  char script_name[] = "/scpt.sh\0";

  if (pipe(link)==-1)
    printf("pipe");

  if ((pid = fork()) == -1)
    printf("fork");


  if(pid == 0) {

    dup2 (link[1], STDOUT_FILENO);
    close(link[0]);
    close(link[1]);
    command_buffer[0] = '\0';
    strcat(command_buffer, in_fsls_directory);
    strcat(command_buffer, script_name);
    printf("%s\n", command_buffer);
    execl("/bin/sh", "sh", command_buffer, (char *)0);

  } else {

    close(link[1]);
    int nbytes = read(link[0], buffer, sizeof(buffer));
    buffer[nbytes - 1] = '\0';

    // LAZY COMPARISON
    // not algorithm name, it's just what im doing.
    // 
    printf("DBG :  %s %d\n", buffer, nbytes);
    if (buffer[0] == 'G') {
      *out_mode = K;
    } else if (buffer[0] == 'K') {
      *out_mode = G;
    } else {
      *out_mode = CLEARED;
    }

    wait(NULL);
  }
}

if i use the same code on running a binary, i am able to read out its output, however, when i try to execute a script such as the one above, i am only able to read the command issued.

example:

excl("binary command to run");
read(output)
output == "correct output"

but if

excl("shell script to run");
read(output)
output == "shell script to run"

why is this?

0 个答案:

没有答案