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 :
-
firstly, *args[i] == '>' ---> this doesn't work . I write a printf to try the if statement is right or not @cadesrtr– esrtr2015-12-05 11:06:46 +00:00Commented Dec 5, 2015 at 11:06
-
can you provide the complete code ?Mayukh Sarkar– Mayukh Sarkar2015-12-05 11:19:36 +00:00Commented Dec 5, 2015 at 11:19
-
your brackets and codeforatting is completely bad...please fix itMayukh Sarkar– Mayukh Sarkar2015-12-05 11:25:49 +00:00Commented Dec 5, 2015 at 11:25
-
I fix my code @MayukhSarkaresrtr– esrtr2015-12-05 11:30:06 +00:00Commented Dec 5, 2015 at 11:30
-
show the complete to so that we can understand it better..Mayukh Sarkar– Mayukh Sarkar2015-12-05 11:37:25 +00:00Commented Dec 5, 2015 at 11:37
3 Answers
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.
1 Comment
(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.
6 Comments
">" instead of > to avoid shell's default redirectionargs[] array.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?
8 Comments
args[i] will be a the address...you need to de-reference it to get the value...hence you need thatchar or int type data and hence for argv, you need to dereference it twice to get a character because it is array of char*..