Trying to understand the UNIX redirection...
Here's what I found in C Primer Plus about redirection:
Quote:
However, I tried the command: Code:
./a.out 0 < /etc/motd The question is: why "<" worked with the file descriptor too? |
Sorry read that incorrectly
correct me if im wrong but wouldent the shell do the redirection first and then run the program second. would put the contents of /etc/motd into 0 ? |
Just to clarify things: in my case, a.out determines the file status flags, using fcntl. It determines the flags well, so I assume the redirection succeded. Am I right? :scratch:
|
It looks like this shell command "./a.out 0 < /etc/motd" would make /etc/motd stdin for the running process invoked as "./a.out 0". If you didn't mean to put a space in between the digit and the redirector, ala "./a.out 0< /etc/motd" it would use /etc/motd for file descriptor zero (which is the same as stdin) and run "./a.out".
|
I understand that, but I don't know why it's happening... stdin is a pointer to a struct (FILE , I guess) and it has nothing to do with file descriptors, no? From what I know, STDIN_FILENO (file descriptor related) != stdin
|
All programs get three default file descriptors. These have numeric IDs as well as names in C land:
Code:
stdin - read mode, number 0 You can ask the shell to over-ride these defaults using re-direction. Changing the destination for stdout and stderr is called output redirection. You do this by using the > symbol and then naming a file to which the output will be written. You explicitly say which file descriptor you want to re-direct by prefixing the > with the number for the file descriptor, e.g. 2> to re-direct stderr. If you don't specify the file descriptor number, 1 (stdout) will be re-directed. For example: Code:
myprogram > myfile.out Code:
myprogram > myfile.out 2> myfile.err You can also say something like this: Code:
myprogram > output 2>&1 You can also re-direct input, so instead of reading from the keyboard, the program will read from the file. You do it like this: Code:
myprogram < myfile.in You can do them all in one command to different files if you like: Code:
myprogram < myfile.in > myfile.out 2> myfile.err Code:
myprogram 1< myfile.in |
Quote:
|
Quote:
When you redirect, the shell will create two processes: the output process and the input process. It will create an unnamed fifo between them and the processes will run independently. When you do 'myprogram < myfile.in', this is the same as 'cat myfile.in | myprogram' without using the 'cat' program. The '|' can be thought of as a pipe between the first program and the second; the unnamed fifo. Reassigning 'stdin' doesn't have a lot to do with it. When you reassign, all you do is replace the pipe assigned by the shell with another file so that standard input comes from that file vice shell redirection (if any.) ta0kira |
Woow! :) Thanks to all of you a lot for taking time to explain to me! I really appreciate your help ;)
|
Yea ive learned alot as well. I was kinda right but not really
|
All times are GMT -5. The time now is 09:20 PM. |