This accepts text as its input. Text output from the command to the shell is delivered via the stdout standard out stream. Error messages from the command are sent through the stderr standard error stream. So you can see that there are two output streams, stdout and stderr , and one input stream, stdin.
Because error messages and normal output each have their own conduit to carry them to the terminal window, they can be handled independently of one another. Streams in Linux—like almost everything else—are treated as though they were files. You can read text from a file, and you can write text into a file. Both of these actions involve a stream of data. Each file associated with a process is allocated a unique number to identify it.
This is known as the file descriptor. Whenever an action is required to be performed on a file, the file descriptor is used to identify the file. These values are always used for stdin , stdout, and stderr :. In a similar vein, when talking about stdin , stdout , and stderr it is convenient to trot out the accepted axiom that a process neither knows nor cares where its three standard streams are terminated.
Should a process care whether its output is going to the terminal or being redirected into a file? Can it even tell if its input is coming from the keyboard or is being piped into it from another process? Actually, a process does know—or at least it can find out, should it choose to check—and it can change its behavior accordingly if the software author decided to add that functionality. The ls command behaves differently if its output stdout is being piped into another command.
And ls does the same thing if its output is being redirected:. You can react to the errors if you need to, as they occur. It also stops the error messages from contaminating the file that stdout has been redirected into. The first line of the script echoes text to the terminal window, via the stdout stream.
This will generate an error message that is delivered via stderr. We can see that both streams of output, stdout and stderr , have been displayed in the terminal windows.
The error message that is delivered via stderr is still sent to the terminal window. We can check the contents of the file to see whether the stdout output went to the file. You can use one of the numeric file descriptors to indicate which standard output stream you wish to redirect. The error message is redirected and the stdout echo message is sent to the terminal window:.
Surely, if we can redirect either stdout or stderr to a file independently of one another, we ought to be able to redirect them both at the same time, to two different files? Yes, we can. This command will direct stdout to a file called capture. Because both streams of output—standard output and standard error—are redirected to files, there is no visible output in the terminal window. We are returned to the command line prompt as though nothing has occurred. The only other combination we can do is to send both stdout and stderr to the same file.
Both the stdout and stderr streams have been redirected to a single destination file. We discussed how a command can detect if any of the streams are being redirected, and can choose to alter its behavior accordingly. Can we accomplish this in our own scripts? And it is a very easy technique to understand and employ.
The clever part is the test within the square brackets. The -t terminal option returns true 0 if the file associated with the file descriptor terminates in the terminal window. How do file descriptors work?
Ask Question. Asked 10 years, 5 months ago. Active 2 years, 8 months ago. Viewed 82k times. Improve this question. Trcx Trcx 3, 5 5 gold badges 28 28 silver badges 30 30 bronze badges.
Add a comment. Active Oldest Votes. File descriptors 0, 1 and 2 are for stdin, stdout and stderr respectively. Improve this answer. That's what I'm looking for! So I need to specify a file for it to use as a temporary storage place with the exec command, and then close them when i'm done? Sorry, I'm a little fuzzy with the exec command, I don't use it much. The file will exist even after your program completes. That could work out nicely then, I'm trying to port some scripts to be compatible with crontab task scheduler, but I am having trouble as cron does not allow for the piping of stdout in scripts.
You don't have to specify a file. It's an old question but one thing needs clarification. What I'd like to point out is that you don't need to change the script :! Your example is actually quite interesting because this script can write to 4 different files:. For example, when I run sudo -s to change user to root, create a directory as root, and try to run the following command as my regular user rsp in my case like this: su rsp -c '..
But even this program:! When run as:. You can even do a more complicated thing. Your original script:! If you want descriptors both 1 and 2 fail, run it like this:. V interesting response.. Thanks — Gunith D. Typo fixed. Thanks for pointing it out. By default cat takes standard input and prints it to the display standard output.
To test this we can use the cat command. Open a terminal and type cat, then type a line from your favorite song and hit enter. Standard input can also come from an input file. If you provide cat with a file as an argument, it will replace or redirect standard input from the keyboard to the data coming from that file.
For example, you can use input redirection to achieve the same results as above like so:. So far we learned how to provide standard input from a keyboard or from a file by argument or redirection.
But, you can also provide standard input from the output of another program on the input stream. This is called piping, and even if you are new to Linux you probably already encountered this concept.
A pipe represented by a vertical line , basically takes output from one command and puts it into the pipe, then it comes out of the pipe on the other side and into the next command. The head program outputs the first 10 lines of a file and pipes it to cat as standard input stdin , which in turn prints it to standard out stdout. Standard output is a standardized stream that is sent to a display monitor by default.
Often the terminal is standard output, but you can pipe or redirect output to a file, another program or device. You can use the output of almost any command as an example of standard output stdout. Here we will use the stat command to get status information about a file and display it on the monitor. As you can see the stat command took the filename as an argument, did some processing and then displayed the statistics of that file to the monitor stdout. You can redirect standard output stdout to a file.
This is useful if you want to save the output for later use, or as a log of a script. For example, you can save the output of the stat command to a file named output. This created a file named output. We can verify this by reading the file. Using this output redirection will overwrite any data that may exist in the file already.
The data pipe sends output from one command to another. Here we will pipe the output of the stat command into grep to pull only last modified time of the file. The stat commands output enters the pipe and passed to grep for processing. When grep finishes processing the data, it sends the output to standard output stdout. Standard error stderr is the destination for all error messages from a process. By default, this is the display screen but just like standard out, it can be redirected.
Mistyping will certainly lead to an error displayed on the screen if you use the command line often. This is an example of standard error stderr. Just like the other streams, you can redirect stderr to a file.
0コメント