case'|': pcmd = (struct pipecmd*)cmd; //fprintf(stderr, "pipe not implemented\n"); // Your code here ... int pipefd[2]; pipe(pipefd);
int process = fork1(); if(process == 0){ // child process // pipe left side close(1); dup(pipefd[1]); close(pipefd[0]); close(pipefd[1]); runcmd(pcmd->left); } else{ // parent process // pipe right side close(0); dup(pipefd[0]); close(pipefd[0]); close(pipefd[1]); wait(0); runcmd(pcmd->right); } break;
手动补全 tt.sh 文件,然后尝试看能否正确运行
1 2 3 4 5 6 7 8
/bin/ls > y /bin/cat < y | /bin/sort | /bin/uniq | /bin/wc > y1 /bin/cat y1 /bin/rm y1 /bin/ls | /bin/sort | /bin/uniq | /bin/wc /bin/rm y