我一直在研究一个自定义 shell 脚本,并且在使用下面给出的代码重定向输出时遇到了一个小错误。在当前状态下,代码可以完美运行,但是当传递给 execvp args 时,会抛出错误,例如:(ls ">" no such file or directory)。我知道这是因为它将整个 args[] 传递给父 shell,而父 shell 不起作用。添加 args[j] = NULL 会消除“<”/ ">”,从而修复错误,但也会导致重定向不再起作用。我怎样才能让它不抛出错误而且还能正常工作?我已经阅读了这个问题的多个版本,但似乎找不到答案。预先感谢您的任何帮助。
switch (fork()){
case -1:
fprintf(stderr, "error forking");
case 0://CHILD
for(int j = 0; j < size; j++){
if(!strcmp(args[j], "<")){//looking for input character
++ext;
if((in = open(args[j+1], O_RDONLY)) < 0){//open file for reading
fprintf(stderr, "error opening file\n");
}
dup2(in, STDIN_FILENO);//duplicate stdin to input file
close(in);//close after use
//args[j] = NULL;
}//end input chech
if(!strcmp(args[j],">")){//looking for output character
++ext;
out = creat(args[j+1], 0644);//create new output file
dup2(out, STDOUT_FILENO);//redirect stdout to file
close(out);//close after usere
// args[j] = NULL;
}//end output check
if(!strcmp(args[j], ">>")){//looking for append
++ext;
int append = open(args[j+1],O_CREAT | O_RDWR | O_APPEND, 0644);
dup2(append, STDOUT_FILENO);
close(append);
// args[j] = NULL;
}
}//end loop
execvp(args[0],args);//execute in parent
fprintf(stderr, "error in child execi \n");//error
exit(0);
default://PARENT
wait(&status); //wait for child to finish
}//end switch
Run Code Online (Sandbox Code Playgroud)
当您解析重定向(例如<
,,>
)>>
并执行您的操作时open/dup2
,您必须将它们从传递给的参数列表中删除execvp
。
因此,给定您的args
,您需要第二个(例如args_clean
)参数列表,您只需复制程序名称及其参数。
并且,您需要额外的增量j
来跳过其中的重定向文件args
(即,只是做j + 1
并不等效)。
这是清理后的子代码[请原谅无偿的清理方式]:
char *args_clean[size];
int cleanidx = 0;
for (int j = 0; j < size; j++) {
if (!strcmp(args[j], "<")) { // looking for input character
++j;
if ((in = open(args[j], O_RDONLY)) < 0) { // open file for reading
fprintf(stderr, "error opening file\n");
}
dup2(in, STDIN_FILENO); // duplicate stdin to input file
close(in); // close after use
continue;
} // end input chech
if (!strcmp(args[j], ">")) { // looking for output character
++j;
out = creat(args[j], 0644); // create new output file
dup2(out, STDOUT_FILENO); // redirect stdout to file
close(out); // close after usere
continue;
} // end output check
if (!strcmp(args[j], ">>")) { // looking for append
++j;
int append = open(args[j], O_CREAT | O_RDWR | O_APPEND, 0644);
dup2(append, STDOUT_FILENO);
close(append);
continue;
}
args_clean[cleanidx++] = args[j];
} // end loop
args_clean[cleanidx] = NULL;
execvp(args_clean[0], args_clean); // execute in parent
fprintf(stderr, "error in child execi \n"); // error
exit(0);
Run Code Online (Sandbox Code Playgroud)
另外,请参阅我的答案,了解与管道类似的内容:fd Leak,custom Shell
并且,对于完整的 shell,请参阅我的答案:Implementing input/output redirection in a Linux shell using C并查看嵌入的 Pastebin 链接