4.4.2. IO-redirection¶
The shell is also responsible for the redirection of input to and output from the commandline. Normally, input comes from the keyboard and output goes to the screen. But it is possible to send the output of command to a file or to get input to a command from a file.
% ls -l > ls.out
sends the output of the command ls -l
to the file “ls.out”. If the file
“ls.out” already exists, it will be overwritten. To append to a file, use the
following:
% ls -l >> ls.out
In fact, the output that appears on the screen when executing a command, is a combination of two output streams: standard out (stdout) and standard error (stderr). The first one is the actual output of the command, e.g. the list of files with the ls-command. The second one, stderr, are the error messages of the command, e.g. a warning about a non-existent file in the ls command.
Normally both streams (or channels) come together on your screen. The ‘>’ redirects stdout to a file, but stderr will still be sent to the screen.
Both streams have a number: stdout 1 and stderr 2. By putting this number before the ‘>’ in a redirection, it is possible to redirect one of both streams. It is also possible to redirect both streams to different files.
command |
result |
---|---|
command > output |
stdout to output, stderr to screen |
command >> output |
stdout to output (appending), stderr to screen |
command 1> output |
stdout to output, stderr to screen |
command 2> error |
stdout to screen, stderr to error |
command > output 2>error |
stdout to output, stderr to error |
command > output 2>>error |
stdout to output, stderr to error (appending) |
command >output 2>&1 |
stdout to output and stderr to the same destination as channel 1 (stdout). Both stdout and stderr to output |
There is also an input channel, stdin (channel 0). You can redirect from this channel by using ‘<’ (which is the same as ‘0<’). Command that accept input from stdin can in this way read input from a file. An example is the mail command that normally reads the message from stdin (the keyboard). You can also first write an email in a separate file and then execute
mail recipient < message
which reads the message from the file “message”.
Another form of stdin-redirection is <<word
, which reads from stdin till a
line with only “word” on it is typed. We will see later how we can use this. Here we give just an example:
% cat <<EOF > message
This is a message
It ends with a line consisting only of EOF
like the next one
EOF
%
This is called a “here”-document.
Exercises
Working further on the exercises of previous section, in a shell script used to run on our cluster, the output and the errors of a command are written to two different files. Write some relevant lines to implement this. The output and the error file should come in the directory where the input files of the project are located. The command itself should run in a temporary directory.