CISC 3110

Unix
Redirection


Unix

More on Redirection

We've redirected output. I may have briefly mentioned redirecting standard input (cin):
java SomeClass <indata.txt
runs the executable a.out, with the standard input (cin) coming from the file indata.txt (rather than the console). This is known as input redirection.

As it turns out, Unix associates a program's files and standard streams) with integers (known as file descriptors). (The standard streams are standard input, standard output, and standard error &mdash. They are known to C++ programmers as cin, cout, and cerr; and in Unix as standard input, standard output, and standard error.) In particular it uses the following associations:
stream file descriptor
standard input / stdin / cin 0
standard output / stdin / cout 1
standard error / stderr / cerr 2
The actual form of the redirection operator in the shell is:

file-descriptor>
Thus: There's one last useful tidbit — writing
java SomeClass >out.txt 2>&1;
will redirect BOTH stdout AND stderr to the same file (the >out.txt redirects standard output to the file out.txt, and the 2>&1; redirects standard error (file descriptor 2) to the same file as file descriptor 1 (standard output)).