Pipes, dup2 and exec()

后端 未结 3 1028
无人及你
无人及你 2020-12-03 05:53

I have to write a shell that can run pipes. For example commands like ls -l | wc -l\". I have successfully parsed the command given by the user as below:

<
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 06:47

    Well, a simple and efficient work arund for something like this is making a script with the pipe stuff and then calling the script with some exec command from your C code.

    script.sh

    #!/bin/sh
    ls -l | wc -l
    

    And the you just do in your C program something like this

    char *argv[] = {"script.sh", NULL};
    execv(argv[0], argv);
    

    Note that you'll have to have the script.sh in the same directory of your C program.

提交回复
热议问题