I implement a simple shell. I want to use input/ output redirection. I write some piece of code but my code doesn't work. here is my code :
You're duplicating the FD into FD 0, which is stdin
. stdout
is FD 1. You should also use dup2
, so you can specify explicitly which FD to assign to, and the macro that holds the FD.
dup2(fd, STDOUT_FILENO);
You should also change
if (*args[i] == '>')
to
if (strcmp(args[i], ">") == 0)
Otherwise it matches any argument that begins with >
, even if it has other characters after it.
(get used writing %s\\n
in your printf statements)
When your compiled program is called myshell, you will see the > when it is given as an argument:
./myshell arg1 arg2 ">" arg4
When you don't quote the >
, the shell will take care of the redirection:
# Not what you want:
./myshell arg1 arg2 > arg4
would result in myshell being called with parameters arg1 and arg2, and the result of myshell will be redirected to arg4.
i think that u do not need asterisk* in your If condition. You want to to compare value of args[x] with a sign '>'.
In case it's not it, Can you write little more about the error which occurs?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.