esrtr
esrtr

Reputation: 91

input output redirection in shell c

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 :

Upvotes: 1

Views: 437

Answers (3)

Barmar
Barmar

Reputation: 780663

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.

Upvotes: 2

Walter A
Walter A

Reputation: 19982

(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.

Upvotes: 1

Dominik
Dominik

Reputation: 331

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?

Upvotes: -2

Related Questions