Читайте также:
|
|
Standard input / output device provides some basic facilities of input-output. These are the three standard streams: input, output and error. Under the agreement, the standard output stream contains all the "normal" output of the program, while the standard error consists of error messages. By default the standard input program is a keyboard, and standard output and error are sent to the screen.
At the time of receipt of the standard input stream (Fig. 6) or divert a file (Fig. 7), the kernel assigns each file number, or tag:
- standard input (stdin) receives a file descriptor 0 (associated with the file from the process reads the data);
- standard output (stdout) receives a file descriptor 1 (associated with the file where the process of recording their answers);
- standard error (stder) receives a file descriptor 2 (linked to the tether, where the process sends error messages).
Figure 6 - Opening of the standard I / O streams without forwarding commands
2.2 Pereadrssatsiya standard input / output
The main advantage of standard I / O is the ability to redirect input or output from the terminal to a file (Fig. 7).
Figure 7 - Standard output redirected to a file
Despite the fact that the standard I / O is a key element of UNIX, the syntax to redirect depends on the type of interpreter shell (Table 19).
Table 19 - Common ways to redirect standard input-output
Action | csh | sh |
Redirect stdout to file Redirect stderr to file Redirect stdout and stderr to file Reading stdin from file | program> file program> & file program <file | program> file program 2> file program> file 2> & 1 program <file |
Redirect stdout to end of file Redirect stderr to the end of file Redirect stdout and stderr to the end of file | program>> file program>> & file | program>> file program 2 >> file program>> file 2> & 1 |
Reading stdin from the keyboard until will be entered a symbol c | program>> c | program>> c |
Send stdout to channel Send stdout and stderr to a channel | program □ program 2 program □ & program 2 |
Consider a few examples of redirection of standard input-output:
1. To send a file by e-mail user joan, using the operator <filename that tells the interpreter shell, that the standard input program mail instead of a keyboard, connect the file:
% Mail joan <myfile
2. To send output commands in a file rather than print them on the terminal must be:
% Cat filel file2> file 3
The most common form of redirection in UNIX is a channel (denoted by the operator | (vertical bar)).
For example, to send a correspondent joan files file1 and file2 in one e-mail message, you must enter the following:
% cfn file1 file2 | mail joan
Secure forwarding of I / O
Setting variable noclobber prevent accidental destruction of data file with redirection of standard output. Consider the following scenario:
% Command> output_file
This line command deletes the old output file and creates a new one. If you or falsity enter the file name, or forgot that the file already exists and contains important data, or going to type>> instead of>, then, unfortunately, your old data will be destroyed.
Дата добавления: 2015-11-14; просмотров: 34 | Нарушение авторских прав
<== предыдущая страница | | | следующая страница ==> |
Key provisions | | | Variable noclobber |