|
CSC128,
: Introduction to UNIX
I/O Redirection
Standard Input/Output
When a UNIX command-line
program (like ls, cat, head, tail
, etc.) is executed, by default
it reads input from the standard input, stdin, which is usually the terminal keyboard.
It writes its output by default to the standard output, stdout,
which is usually the
terminal screen.
For example, if we type the command cat by itself, the program (cat ) will try to read its input from
the terminal keyboard, until it reaches the end-of-file (EOF) character,
which is Ctrl-D. As usual, it will copy its input (stdin ) to its output (stdout ). When you specify file(s)
to cat , it reads
from them instead of stdin .
So what do you do when you would like cat to write to a file instead of stdout ?
Redirecting Output
You can redirect
the command's output from stdout to a file by using > or >>. Instead of the command's output being
sent to the terminal screen, it will be sent to the file named after the
>
or >>. To use the command cat to type words directly to a file,
you can redirect the output from stdout to the file like this:
cat > test.txt
Since no filename is
specified to the cat command, it reads input from the terminal
keyboard (stdin
) until it reaches and EOF character (Ctrl-D). The > redirects output from the terminal
screen (stdout
) to the file specified, test.txt .
This will create the
file test.txt or overwrite it if it exists. To
append to the end of a file instead of overwriting it, use
>> instead.
Redirecting stderr (Standard Error)
2>&1 > /dev/null Will concatenate both stderr and stdout and send both to /dev/null See p. 309 in the book for this.
2>/dev/null Will redirect stderr to /dev/null
Redirecting Input
Redirecting
input is similar in form to redirecting output. An example:
cat < test.txt
This will redirect
the command's input to read from the file named test.txt instead of reading from stdin .
Another example:
smauney@sol:~ $ ls
public projects stuff example_file
smauney@sol:~ $ rm example_file
/bin/rm: remove `example_file'? n
smauney@sol:~ $ cat y.txt
y [Enter]
[Ctrl-D]
smauney@sol:~ $ rm example_file < y.txt
/bin/rm: remove `example_file'?
smauney@sol:~ $ ls
public projects stuff
smauney@sol:~ $
When
stdin is redirected
from y.txt, you
don't even get the chance to say no. The rm command looks to stdin , sees a "y" and a newline from having
pressed the enter key, and dutifully removes the file. Since stdin is normally the terminal keyboard,
rm assumes
that the response is being interactively typed at the keyboard, although
it isn't
Pipes
A pipe
(|) connects the
standard output of one command directly to the standard input of another.
An example:
smauney@sol:~/public $ ls
harold harry lab1 poem richard stuff
thomas verse
smauney@sol:~/public $ ls | sort
harold
harry
lab1
poem
richard
stuff
thomas
verse
smauney@sol:~/public $
The
ls command lists the files
in the directory to stdout . The pipe (|) redirects the stdout of ls to the
stdin of sort . Since the output of sort isn't redirected, it is sent to
stdout
, which is the terminal screen. Another example:
>ls | sort | grep ha
harold
harry
richard
After the second escape character \ there is also a space. The \ tell the command to use the next character as a regular character
The
grep command only sends
lines that contain the letter sequence "ha" to stdout . The output of ls is redirected to the input of sort.
sort 's output
is redirected to the input of grep . grep sends its output to stdout , the terminal screen.
Filters
A filter
is a command that processes an input stream of data and produces an output
stream of data. In the above example, sort and grep are examples of commands that are
filters.
Filters are used often in UNIX. You
can string together many filters with pipes to produce the
desired output.
Tee
tee
is a special filter that takes the data it gets from stdin and both redirects it to a file
and its stdout . An example:
>ls | sort | tee list | grep
ha
harold
harry
richard
smauney@sol:~/public $ cat list
harold
harry
lab1
poem
richard
stuff
thomas
verse
smauney@sol:~/public $
The file list was created by tee before the
grep command
had a chance to modify the data stream.
|