A neat trick in Linux is that the input and output of a program can be redirected from/to typical files. This short note will show you some of the stream redirection you can do on the command line, which may help you to generally deal with program input and ouput better and specifically to build test suites for text-based programs.
This note starts with the short version of working with streams on the command line and within programs, then gives a little background for those interested in why things work this way.
Output from Linux programs can be redirected to a file by invoking the command with extra redirection specifiers, like so:
./prog >& saved.txt
Whatever output that prog
would have
otherwise printed to the console will overwrite the
file saved.txt
.
Most programs make a distinction between their
error messages and their regular output. Those
two streams of data can be redirected separately,
using 1>
for regular output and
2>
for error output. For example:
./prog 1> log.txt 2> err.txt
log.txt
with the
regular output of ./prog
and will
overwrite the file err.txt
with any
error messages that ./prog
shows.