Active 29 days ago. Viewed k times. Improve this question. Lee Goddard 9, 3 3 gold badges 44 44 silver badges 55 55 bronze badges. Add a comment. Active Oldest Votes. Improve this answer. Mat Mat k 39 39 gold badges silver badges bronze badges. Search for "shell redirection" for more details. Show 7 more comments. Costi Ciudatu Costi Ciudatu Not sure when this operator was added but it may not be available in older versions of Bash. It does appear to be working on my machine which runs Gnu bash v3.
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. If stdin is connected to a terminal window the test will prove true. If stdin is connected to a file or a pipe, the test will fail.
Redirecting stdout and stderr to a file: As redirection is a method of capturing a program output and sending it as an input to another command or file. You can also use 1 instead of 2 so that stdout gets redirected to the 'file'. Improve this answer. If stderr is redirected to stdout first, use the below-given command to redirect the stdout to a file. The only other combination we can do is to send both stdout and stderr to the same file. We can achieve this with the following command:.
I just came up with a solution for sending stdout to one command and stderr to another, using named pipes. Here goes.
0コメント