linux脚本实现多重管道,制作Linux shell时流重定向和管道

我有一个在C中创建Linux shell的任务.目前,我仍然坚持实现重定向和管道.我到目前为止的代码如下.main()解析用户的输入.如果内置命令,则执行该命令.否则,标记化的输入传递给execute()(我知道我应该把内置命令拉到自己的函数中).

execute()的作用是遍历数组.如果遇到或者|它应该采取适当的行动.我试图正常工作的第一件事是管道.不过,我肯定做错了,因为即使是一个烟斗也无法让它工作.例如,一个示例输入/输出:

/home/ad/Documents> ls -l | grep sh

|: sh: No such file or directory

|

我的想法是让每个方向和管道仅适用于一个案例,然后通过使函数递归,我希望在同一命令行中使用多个重定向/管道.例如,我可以做program1 < input1.txt > output1.txt或ls -l | grep sh > output2.txt.

我希望有人可以在尝试管道时指出我的错误,并且可能提供一些关于如何处理用户输入多个重定向/管道的情况的指示.

#include

#include

#include

#include

#include

int MAX_PATH_LENGTH = 1024; //Maximum path length to display.

int BUF_LENGTH = 1024; // Length of buffer to store user input

char * delims = " \n"; // Delimiters for tokenizing user input.

const int PIPE_READ = 0;

const int PIPE_WRITE = 1;

void execute(char **argArray){

char **pA = argArray;

int i = 0;

while(*pA != NULL) {

if(strcmp(argArray[i],"

printf("

}

else if(strcmp(argArray[i],">") == 0) {

printf(">\n");

}

else if(strcmp(argArray[i],"|") == 0) {

int fds[2];

pipe(fds);

pid_t pid;

if((pid = fork()) == 0) {

dup2(fds[PIPE_WRITE], 1);

close(fds[PIPE_READ]);

close(fds[PIPE_WRITE]);

char** argList;

memcpy(argList, argArray, i);

execvp(argArray[0], argArray);

}

if((pid = fork()) == 0) {

dup2(fds[PIPE_READ], 0);

close(fds[PIPE_READ]);

close(fds[PIPE_WRITE]);

execvp(argArray[i+1], pA);

}

close(fds[PIPE_READ]);

close(fds[PIPE_WRITE]);

wait(NULL);

wait(NULL);

printf("|\n");

}

else {

if(pid == 0){

execvp(argArray[0], argArray);

printf("Command not found.\n");

}

else

wait(NULL);*/

}

*pA++;

i++;

}

}

int main () {

char path[MAX_PATH_LENGTH];

char buf[BUF_LENGTH];

char* strArray[BUF_LENGTH];

/**

* "Welcome" message. When mash is executed, the current working directory

* is displayed followed by >. For example, if user is in /usr/lib/, then

* mash will display :

* /usr/lib/>

**/

getcwd(path, MAX_PATH_LENGTH);

printf("%s> ", path);

fflush(stdout);

/**

* Loop infinitely while waiting for input from user.

* Parse input and display "welcome" message again.

**/

while(1) {

fgets(buf, BUF_LENGTH, stdin);

char *tokenPtr = NULL;

int i = 0;

tokenPtr = strtok(buf, delims);

if(strcmp(tokenPtr, "exit") == 0){

exit(0);

}

else if(strcmp(tokenPtr, "cd") == 0){

tokenPtr = strtok(NULL, delims);

if(chdir(tokenPtr) != 0){

printf("Path not found.\n");

}

getcwd(path, MAX_PATH_LENGTH);

}

else if(strcmp(tokenPtr, "pwd") == 0){

printf("%s\n", path);

}

else {

while(tokenPtr != NULL) {

strArray[i++] = tokenPtr;

tokenPtr = strtok(NULL, delims);

}

execute(strArray);

}

bzero(strArray, sizeof(strArray)); // clears array

printf("%s> ", path);

fflush(stdout);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值