我正在制作一个外壳(效果很好)。但是,现在我正在尝试实现输出重定向。 cat test1.txt > text2.txt
。如果我在不重定向的情况下运行命令,那么它将运行完美。那么我的重定向输出代码中缺少什么?
Text1.txt
This is some dummy text
我现在运行了我的外壳,如下所示
$shell cat test1.txt > text2.txt
Executing cat
Everything went well!
$shell
现在,如果我打开text2.txt。这就是它的内容
Executing cat
This is some dummy text
Everything went well!
我的重定向代码
char **mycommand = {"cat", "text1.txt", ">", "text2.txt"};
if (strcmp(mycommand[2], ">") == 0) {
int fd = open(mycommand[3], O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
dup2(fd, STDOUT);
mycommand[2] = '\0';
break;
}
// Then it does all the execution stuff on mycommand