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

 2023-09-23 阅读 20 评论 0

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

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

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

shell管道符,/home/ad/Documents> ls -l | grep sh

|: sh: No such file or directory

|

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

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

#include

linux创建shell脚本,#include

#include

#include

实现shell、#include

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

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

linux执行脚本,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);

}

}

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/89441.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息