Shell中的输入和输出重定向

时间:2018-11-20 05:18:50

标签: c shell io-redirection

我正在尝试实现输入和输出重定向为C。我阅读了此问题的所有答案,但它不起作用!如果(execvp,则转到最后一个。

我想要这样的东西:

输出重定向:ls -al > output.txt

输入重定向:sort < input.txt

这是我编写的代码:

int io_redirection(char * args[], char* inputFile, char* outputFile, int option){

    int err = -1;

    int fileDescriptor = -1; // between 0 and 19
    pid_t pid = -10;
    pid = fork();

    if(pid==-1){
        printf("Child process could not be created\n");
        return;
    }
    if(pid==0){
        // Option 0: output redirection
        if (option == 0){
            // I also tried fileDescriptor = creat(outputFile, 0644);
            fileDescriptor = open(outputFile, O_CREAT | O_TRUNC | O_WRONLY, 0600); 
            dup2(fileDescriptor, STDOUT_FILENO); 
            close(fileDescriptor);


        }
         else if (option == 1){
            // Option 1: input redirection

            fileDescriptor = open(inputFile, O_RDONLY, 0600);  
            dup2(fileDescriptor, STDIN_FILENO);
            close(fileDescriptor);

        }



        if (execvp(args[0],args)==err){
            printf("err");
            kill(getpid(),SIGTERM);
        }        
    }
    waitpid(pid,NULL,0);
}

用于输出重定向args包含ls-al。对于输入一个,它包含sort

2 个答案:

答案 0 :(得分:1)

execvp需要NULL作为最后一个参数。我猜想您的命令综合将忽略该命令,其余命令将按预期工作。

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
#include<signal.h>
#include<sys/wait.h>


void print_child_status (int status) {
  if (WIFEXITED (status)) {
    fprintf (stdout, "Child exited with status %d\n", WEXITSTATUS (status));
  } else if (WIFSTOPPED (status)) {
    fprintf (stdout, "Child stopped by signal %d (%s)\n", WSTOPSIG (status), strsignal (WSTOPSIG (status)));
  } else if (WIFSIGNALED (status)) {
    fprintf (stdout, "Child killed by signal %d (%s)\n", WTERMSIG (status), strsignal (WTERMSIG (status)));
  } else {
    fprintf (stdout, "Unknown child status\n");
  }
}

int io_redirection(char * args[], char* inputFile, char* outputFile, int option){

    int err = -1;

    int fileDescriptor = -1; // between 0 and 19
    pid_t pid = -10;
    pid = fork();

    if(pid==-1){
        printf("Child process could not be created\n");
        return -1;
    }
    else if(pid == 0){
        // Option 0: output redirection
        if (option == 0){
            // I also tried fileDescriptor = creat(outputFile, 0644);
            fileDescriptor = open(outputFile, O_CREAT | O_TRUNC | O_WRONLY, 0600); 
            dup2(fileDescriptor, STDOUT_FILENO); 
            close(fileDescriptor);


        }
         else if (option == 1) {
            // Option 1: input redirection
            fileDescriptor = open(inputFile, O_RDONLY);
             printf("Input redirection %d\n", fileDescriptor);
            dup2(fileDescriptor, STDIN_FILENO);
            close(fileDescriptor);

        }
        err = execvp(args[0], args);

        if (err){
            printf("err %d\n", err);
        }
        return err;
    } else {
        int status;
        waitpid(pid, &status,0);
        print_child_status(status);
    }
    return 0;

}

int main() {
    char * args[] = {"ls", "-l", NULL};
    io_redirection(args, NULL, "out.txt", 0);
    char * args1[] = {"grep", "main", NULL};
    io_redirection(args1, "./test.c", NULL, 1);
    return 0;
}

答案 1 :(得分:1)

也许您的测试有误,我使用以下code测试是正确的。

int main() {
  char* argv[] = {"ls", "-al", NULL};
  io_redirection(argv, NULL, "test", 0);

  return 0;
}