LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-02-2012, 01:02 PM   #1
qwerty4061
Member
 
Registered: Nov 2011
Posts: 31

Rep: Reputation: Disabled
/dev/stdout confusion


Hi,
I am having some confusion regarding the device file /dev/stdout (and stdin). I am not understanding the purpose of having a file like this. As I understand stdout and stdin are redirected to some device like /dev/tty or a serial port. So what purpose is served by having 2 extra device files.

ls > /dev/stdout and ls >/dev/tty gives the same output.

Also if I do something like this
fgets(str, 80, stdin); inside a code, what does stdin mean here.

Thanks
 
Old 01-02-2012, 04:55 PM   #2
hydraMax
Member
 
Registered: Jul 2010
Location: Skynet
Distribution: Debian + Emacs
Posts: 467
Blog Entries: 60

Rep: Reputation: 51
Quote:
Originally Posted by qwerty4061 View Post
Hi,
I am having some confusion regarding the device file /dev/stdout (and stdin). I am not understanding the purpose of having a file like this. As I understand stdout and stdin are redirected to some device like /dev/tty or a serial port. So what purpose is served by having 2 extra device files.

ls > /dev/stdout and ls >/dev/tty gives the same output.

Also if I do something like this
fgets(str, 80, stdin); inside a code, what does stdin mean here.

Thanks
I'll admit it is a little vague to me, but as I understand it: /dev/stdout actually points to /proc/self/fd/1 (at least on my system), so it points to the standard out stream for your process, whatever that happens to be. Whereas, I believe, /dev/tty points to the controlling terminal for the process. Maybe this illustration code helps:
Code:
$ cat testout.sh
echo "hi there" >> /dev/tty
$ bash testout.sh >> testout
hi there
$ cat testout
(nothing is printed)
So here, I have a script that sends "hi there" to the controlling terminal. Then, I run the script, but I direct the standard output of the script to a file called "testout". The words "hi there" are in fact printed to my controlling terminal, but nothing is sent to the file "testout". So, I believe the answer is that standard out and your controlling terminal don't have to be the same. (Somebody out there correct me if I have been technically inaccurate...)

To your second question, "stdin" is a FILE stream defined in "stdio.h". From STDIN(3):

Code:
       Under  normal circumstances every UNIX program has three streams opened for it when it starts up, one for input, one for output, and one
       for printing diagnostic or error messages.  These are typically attached to the user's terminal (see tty(4) but might instead  refer  to
       files or other devices, depending on what the parent process chose to set up.  (See also the "Redirection" section of sh(1).)

       The  input  stream  is  referred  to as "standard input"; the output stream is referred to as "standard output"; and the error stream is
       referred to as "standard error".  These terms are abbreviated to form the symbols used to refer to these files,  namely  stdin,  stdout,
       and stderr.
The files /dev/stdout, /dev/stdin, and /dev/stderr I believe are provided simply for convenience. In programming you'll just use functions like fgets, printf, and so forth.
 
Old 01-03-2012, 10:15 AM   #3
qwerty4061
Member
 
Registered: Nov 2011
Posts: 31

Original Poster
Rep: Reputation: Disabled
Thanks for your answer. How did you find out that /dev/stdout is pointing to /proc/fd/1. I can see that in my PC using the command
"file /dev/stdout". But I have an embedded board also running busybox, and it doesn't have the command "file". The stdout/stdin is happening in the embedded board through the serial port, but I wanted to see where the setting is done.
 
Old 01-03-2012, 11:16 PM   #4
hydraMax
Member
 
Registered: Jul 2010
Location: Skynet
Distribution: Debian + Emacs
Posts: 467
Blog Entries: 60

Rep: Reputation: 51
On my system it is just a shortcut to /proc/self/fd/1:

Code:
$ ls -lh /dev/stdout 
lrwxrwxrwx 1 root root 15 Dec 30 21:54 /dev/stdout -> /proc/self/fd/1
You can also see what terminal is connected to stdin with this command:

Code:
$ tty
/dev/pts/1
 
  


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
WARN: stdin/stdout is not a TTY; using /dev/console alirezan1 Linux - Newbie 4 06-07-2010 09:00 PM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 02:36 AM
/dev/stdout nbframer Linux - Newbie 6 09-10-2005 04:48 PM
Switch stdout between /dev/null and a file? gremlin2000 Linux - General 2 06-08-2004 07:12 AM
Switch stdout between /dev/null and a file? gremlin2000 Linux - Newbie 1 06-01-2004 10:13 AM


All times are GMT -5. The time now is 06:29 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration