Wednesday, August 12, 2009

Redirections

Redirection

In this we shall look at redirection of output.

There are three channels that we are aware of,

* stdIn 0 Standard Input
* stdOut 1 Standard Output
* stdErr 2 Standard Error



OUTPUT >, >>, >&

So when you run an application or command on the prompt, The keyboard acts as the stdIn, the terminal is the stdOut and in many cases it is also the stdErr

[student@localhost ~]$ command [arg]



In some cases you might want to capture the output to a file, so the way to do that is

[student@localhost ~]$ command > filename

this shall redirect all output to a file specified as filename and there shall be nothing displayed on the screen. This is the same as specifying

[student@localhost ~]$ command 1> filename

Note that the 1> does not have a space inbetween, if there would be a space then it would be considered as an argument. You could do this with any of the output channels, so you could redirect the stdOut to a file and stdErr to another file. simply by using 1> and 2>

Now, let us look at if you wanted to redirect the stdErr to stdOut (and they were set to be different terminals) you can redirect 2 (stdOut) to 1 (stdIn)

[student@localhost ~]$ command 2> 1

But the flaw with this command is that *nix thinks this is asking you to redirect the stdOut to a file called 1, this is not what we are looking for.

[student@localhost ~]$ command 2>& 1 and hence we use the & sign to specify that this is a channel and not a file

You can have multiple redirections, so take the following example

[student@localhost ~]$ command > filename 2>& 1

this would output the results (stdOut) to a file called filename and the errors (stdErr) to the display terminal (stdIn). Some use case scenarios of such a facility could be when you want to run this command in the background.

NOTE: However the order is most important, if you had used

[student@localhost ~]$ command 2>& 1 > filename

this would be a bit different in the way it works, all the output from the stdErr is being redirected to the stdIn via 2>& 1, in the second bit we are redirecting the stdIn to a file, so what we would get in a file is all the output including the errors, which might not be what we want (or what we want depending on the situation)

Whenever we use the single >, this is redirecting the output to the specified location/channel. Everytime we redirect to a file using the >, the file is overwritten by the new output. If we wish to create like a log file where the data is added to the file rather than overwriting the same, we use the >> redirection.

[student@localhost ~]$ command > filename , would create a file called filename with new contents based on the output of the command.

[student@localhost ~]$ command >> filename , this would append the output of the command to the file called filename which means add to the information that was already there in the file.



INPUT <


Like the output of data, you can also redirect input of data.This could be useful in testing where you might have a file that contains data that you need to test and you can then redirect it to the command using

[student@localhost ~]$ command < input_filename ,where input_filename is the name of the file that is providing the input to the command.

some of you might have used this for testing in CP1300.

No comments:

Post a Comment