LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-16-2006, 01:26 PM   #1
BiThian
Member
 
Registered: Aug 2006
Location: Romania
Distribution: NetBSD 3.1
Posts: 118

Rep: Reputation: 15
Trying to understand the UNIX redirection...


Here's what I found in C Primer Plus about redirection:
Quote:
The < symbol is a Unix and Linux (and DOS) redirection operator. It causes the words file to be associated with the stdin stream.
So, I understood that < works with streams aka an interface(provided by C) to files.
However, I tried the command:
Code:
./a.out 0 < /etc/motd
and the redirection did took place, but in my example 0 is a file descriptor( the low-level equivalent to C stream).
The question is: why "<" worked with the file descriptor too?

Last edited by BiThian; 11-16-2006 at 03:40 PM.
 
Old 11-16-2006, 01:41 PM   #2
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
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 ?

Last edited by exvor; 11-16-2006 at 01:48 PM.
 
Old 11-16-2006, 02:07 PM   #3
BiThian
Member
 
Registered: Aug 2006
Location: Romania
Distribution: NetBSD 3.1
Posts: 118

Original Poster
Rep: Reputation: 15
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?
 
Old 11-16-2006, 04:12 PM   #4
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
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".
 
Old 11-16-2006, 04:25 PM   #5
BiThian
Member
 
Registered: Aug 2006
Location: Romania
Distribution: NetBSD 3.1
Posts: 118

Original Poster
Rep: Reputation: 15
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
 
Old 11-16-2006, 04:39 PM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
All programs get three default file descriptors. These have numeric IDs as well as names in C land:

Code:
stdin  - read mode,  number 0
stdout - write mode, number 1
stderr - write mode, number 2
By default, when you run a program from the shell, the shell connects stdin to the keyboard (for input), stdout and stderr to the terminal's output.

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
This will put the output which myprogram sends to stdout in the file myfile.out. Note that if myprogram sends output to stderr, this will still be sent to the terminal. In borne style shells (e.g. bash - the default shell on many Linux distros), you can do something like this:
Code:
myprogram > myfile.out 2> myfile.err
...which will send stdout to myfile.out, and stderr to myfile.err.

You can also say something like this:
Code:
myprogram > output 2>&1
The 2>&1 says "put output on file descriptor 2 (stderr) in the same place as file descriptor 1", which in this case is the file "output".

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
In this case input is read from the file "myfile.in". By default < puts intput into file descriptor 0. 0< does exactly the same thing, it's just an explicit way of saying it.

You can do them all in one command to different files if you like:
Code:
myprogram < myfile.in > myfile.out 2> myfile.err
Note that if you do this:
Code:
myprogram 1< myfile.in
...it won't work since file descriptor 1 (stdout) has more "write", and so it won't be read from. Same goes for descriptor 2 (stderr).
 
Old 11-16-2006, 04:57 PM   #7
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Quote:
Originally Posted by BiThian
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
But unless you reassign the variable stdin, it indicates the FILE struct which describes file descriptor zero. (That is the default environment. And if you change what file belongs to file descriptor zero, stdin will refer to that particular file.) So most of the time, stdin and file descriptor zero are equivalent, referencing the same resource.
 
Old 11-16-2006, 05:12 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by BiThian
From what I know, STDIN_FILENO (file descriptor related) != stdin
For any file that's opened on your system, there is a numerical file descriptor. Standard input is merely the output end of an unnamed fifo, which is a unique file for each redirection pair (unless you are clever), always numbered 0 for the input half and 1 for the output half, corresponding to the STDIN_FILENO and STDOUT_FILENO macros. In order to use standard input, however, you need a stream, which is what 'stdin' is. Because standard input is open when your program starts, 'stdin' is already assigned.

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
 
Old 11-17-2006, 03:00 AM   #9
BiThian
Member
 
Registered: Aug 2006
Location: Romania
Distribution: NetBSD 3.1
Posts: 118

Original Poster
Rep: Reputation: 15
Woow! Thanks to all of you a lot for taking time to explain to me! I really appreciate your help
 
Old 11-17-2006, 11:17 AM   #10
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Yea ive learned alot as well. I was kinda right but not really
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Speaking UNIX, Part 4: Setting and managing permissions on UNIX LXer Syndicated Linux News 0 10-21-2006 01:54 AM
LXer: UNIX tips: Become a better blogger with UNIX LXer Syndicated Linux News 0 10-12-2006 04:33 AM
What way is best for understand OS (linux or unix) kernel structure Nad0xFF Programming 11 04-06-2005 01:19 PM
Why did you experienced users of Unix change to unix over Windows? Laptop2250 Linux - General 11 10-28-2003 11:51 AM
How to schedule unix script periodically from unix os level??? gopi_20us Programming 2 03-11-2002 06:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration