1

I have implemented a shell which supports redirection but after the redirtection is done it gets of of my shell. How can I manage it in a way to get back to shell (stdout)?

 int i;
     for (i=1; args[i];i++)
         {
           if (strcmp(args[i],">")==0)
             {
               printf("argv[i] %s %d \n", args[i], i);
               int out = open(args[i+1], O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
               close(1);
               int fdl=dup2(out,1);
               close(out);
               execvp(args[0],args);
            //   open(STDOUT, ">>", \$out); //doesn't work~!
             }

         }

Here's what happens when I execute my shell:

 ./basic_shell 
mysh> pwd > out_pwd
argv[i] > 1 
pwd: ignoring non-option arguments

and it creates out_pwd as expected and writes the pwd result into it. However when I try

mysh>ls > out_ls

I receive this error:

ls cannot access >: No such file or directory

Can you please give me some hints on how to fix it?

4

2 回答 2

2

您不想在 shell 进程中实际转换文件描述符,而是希望简单地保存映射的表示,并在命令之后fork但之前在子进程中执行替换exec

于 2013-09-27T00:22:27.220 回答
0

先关闭句柄
然后调用kernel32中的AllocConsole/或者win 7^中的console api来创建console

于 2013-09-26T23:52:06.997 回答