Page 252 - DCAP403_Operating System
P. 252
Unit 13: Case Study: Linux
Standard Input-output Notes
Once a command is run, a process is created. This process then opens three fl ows:
1. stdin, called the standard input, where the process will read the input data. By default
stdin refers to the keyboard; STDIN is identified by the number 0;
2. stdout, called standard output, where the process will write the output data. By default,
stdin refers to the screen; STDOUT is identified by the number 1;
3. stderr, called standard error, where the process will write error messages. By default, stderr
refers to the screen. STDERR is identified by the number 2;
Process
STDIN STDOUT
STDERR
By default, whenever a program is run data is read from the keyboard and the program sends its
output and errors to the screen. However, it is also possible to read data from any input device,
even a file, and send the output to a display device, a fi le, etc.
Redirections
Like any Unix type system, Linux has mechanisms which make it possible to redirect the standard
input-output to fi les.
So, using the “>” character makes it possible to redirect the standard output of a command on the
left to the file located on the right:
ls -al /home/jf/ > toto.txt
echo “Toto” > /etc/myconfi gurationfi le
The following command is equivalent to a copy of the fi les:
cat toto > toto2
The purpose of the “>” redirection is to create a new file. So, if a file with the same name already
exists it will be deleted. The following command simply creates an empty fi le:
> fi le
Using the double character “>>” makes it possible to add the standard output to the file, i.e. add
the output after the file without deleting it.
In the same way, the “<” character indicates a redirection of the standard input. The following
command sends the content of the toto.txt file to the input of the command cat, the only purpose
of which is to display the content on the standard output (example not useful, but instructive):
cat < toto.txt
Finally, using the “<<” redirection makes it possible to read on the standard input, until the
string located to the right is found. In the following example, the standard input will be read until
the word STOP is found, and then the result will be displayed:
cat << STOP
LOVELY PROFESSIONAL UNIVERSITY 245