![]() |
Computer Systems Lab CS1000 |
![]() Wiscinfo |
![]() CS Home |
![]() CSL |
![]() CS1000 |
![]() Feedback |
Many programs in UNIX will by default read their input from the user's keyboard and write their output to the terminal. If you write a C program that uses printf and scanf, the program will act this way. This is convenient for debugging, but what about when it comes time to actually test it with your TA's data? In most systems, you would have to recompile your program. Under UNIX, though, you can dynamically redirect the normal input and output streams right on the command line without ever touching the program's code. For example,
vega1% prog1 < test-data
runs prog1, taking its input from the named file test-data. The output will still be printed on the user's terminal. On the other hand,
vega1% prog1 < test-data > results
still reads from test-data; additionally, it sends the output to the file results. If you had said:
vega1% prog1 < test-data >> results
then the output of the prog1 program would have been appended to the file results, rather than replacing whatever was already there.
Occasionally you may have a program that produces an enormous amount of useless output. You can redirect the output of this program to /dev/null, the little black hole of UNIX. Anything sent here is lost forever. You can even copy files to /dev/null, but this is rather useless.
You may supply the redirections in either order if both are present; the only general rule is that they should follow all regular arguments. This is not technically correct, but you will save yourself some time spent in confusion if you always put the redirection at the end of the command line. For example, the following line mails a file called results to a user named brad at the computer called bay at NYU with the subject test results:
vega1% mail -s "test results" brad@bay.nyu.edu < results