1

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 :

8
  • firstly, *args[i] == '>' ---> this doesn't work . I write a printf to try the if statement is right or not @cad Commented Dec 5, 2015 at 11:06
  • can you provide the complete code ? Commented Dec 5, 2015 at 11:19
  • your brackets and codeforatting is completely bad...please fix it Commented Dec 5, 2015 at 11:25
  • I fix my code @MayukhSarkar Commented Dec 5, 2015 at 11:30
  • show the complete to so that we can understand it better.. Commented Dec 5, 2015 at 11:37

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I apply your solution. There is no error now. Thanks for that. But still I can create a file usin'>' but I can't write anything on that file .
1

(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

can you explain how can I fix this compare if statement
@esrtr you need to use ">" instead of > to avoid shell's default redirection
@Walter A ... Your solution is based on the fact that OP's system is Linux & Unix based however it is platform independent solution
I can create a file but I can't writing anything on that file. Why ?
I don't think he's getting the argument from the shell, the user is typing them into his program and it's splitting them into the args[] array.
|
-2

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

if I don't add * ,it would be an error on terminal : "warning : comparison betwween pointer and integer"
@Dominik args[i] will be a the address...you need to de-reference it to get the value...hence you need that
how is that? we do not know what kind of array args is from that piece of code but in general u can make reference to the value of an array element without *.
Check properly he is comparing '>' which is a char or int type data and hence for argv, you need to dereference it twice to get a character because it is array of char*..
oh fine, that args must be main's parameter **char typically called argv. i haven't understood that properly. thanks.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.