I/O Redirection¶
Standard I/O Streams¶
Three Standard I/O file descriptors
By default bound to the terminal ⟶ keyboard and screen
Meaning |
Name |
Descriptor |
C Macro |
---|---|---|---|
Standard input |
|
0 |
|
Standard output |
|
1 |
|
Standard error |
|
2 |
|
Important
A tradition which is religiously adhered to in Unix
Debug and error output goes to standard error ⟶ can be redirected to logfile, for example (or discarded into
/dev/null
)Data goes to standard output ⟶ can be fed into another programm via the pipe
I/O Redirection Operators¶
|
|
|
|
|
|
Variation: non-destructive redirection
>
truncatesfile
prior to writing, if it exists>>
appends tofile
(or creates if it does not exist)
Example: Standard Output Redirection¶
Say we are using the grep
program to search /etc/passwd
for
lines containing the word jfasch
. It writes the lines (only one in
out case) that it finds to standard output:
$ grep jfasch /etc/passwd
jfasch:x:1000:1000:Joerg Faschingbauer:/home/jfasch:/bin/bash
Redirect its output into a file that we might keep for later use,
$ grep jfasch /etc/passwd > found.csv
$ ls -l found.csv
-rw-rw-r--. 1 jfasch jfasch 62 May 12 13:28 found.csv
$ cat found.csv
jfasch:x:1000:1000:Joerg Faschingbauer:/home/jfasch:/bin/bash
Example: Standard Input Redirection¶
Although grep
can open its input file by itself when given as a
second commandline parameter, it reads from standard input when not.
$ grep jfasch < /etc/passwd
jfasch:x:1000:1000:Joerg Faschingbauer:/home/jfasch:/bin/bash
Example: Standard Error Redirection¶
grep
emits error messages on standard error in some cases, on of
which is where it cannot find the input file given,
$ grep jfasch /etc/passwd-does-not-exist
grep: /etc/passwd-does-not-exist: No such file or directory
If, for example, we want to keep error messages for later debugging (a larger shell script, say), we can redirect standard error to a file. Remember, standard error is on file descriptor 2.
$ grep jfasch /etc/passwd-does-not-exist 2>errors.lst
$ ls -l errors.lst
-rw-rw-r--. 1 jfasch jfasch 60 May 12 14:21 errors.lst
$ cat errors.lst
grep: /etc/passwd-does-not-exist: No such file or directory
Another way of treating error is to ignore them. In this case, the
character special device /dev/null
is usually used (it behaves
like a filefor the purpose of redirection):
$ grep jfasch /etc/passwd-does-not-exist 2>/dev/null
Example: Everything Redirection¶
If we want (not to say that we have to though!), we can combine
redirections of all three standard streams in a way that grep
Reads from standard input
Writes standard output to a file
Write standard error to
/dev/null
$ grep jfasch < /etc/passwd > found.csv 2>/dev/null
Important
When multiple redirections are used, the operations are executed from left to right!
This might not sound so important from what we’ve learned so far - but see, for example, I/O Redirection: Swap stdout And stderr.