[quote] standard Input and Output Redirection
2013-12-05 21:05
351 查看
From
http://sc.tamu.edu/help/general/unix/redirection.html
Standard Input and Output Redirection
The shell and many UNIX commands take their input from standard input (stdin), writeoutput to standardoutput (
stdout), and write erroroutput to standard error (
stderr). By default, standard input is connected to the terminal keyboard and standardoutput and error to the terminal screen.
The way of indicating an end-of-file on the default standard input, a terminal, is usually <Ctrl-d>.
Redirection of I/O, for example to a file, is accomplished by specifying the destination on the command line using a redirection metacharacter followed by the desired destination.
C Shell Family
Some of the forms of redirection for the C shell family are:Character | Action |
---|---|
> | Redirect standardoutput |
>& | Redirect standardoutput and standard error |
< | Redirect standard input |
>! | Redirect standardoutput; overwrite file if it exists |
>&! | Redirect standardoutput and standard error; overwrite file if it exists |
| | Redirect standardoutput to another command (pipe) |
>> | Append standardoutput |
>>& | Append standardoutput and standard error |
% command -[options] [arguments]<input file >output file
If you are using csh and do not have the noclobber variable set, using
>and
>&to redirectoutput will overwrite any existing file of that name. Setting noclobber prevents this. Using
>!and
>&!always forces the file to be overwritten. Use
>>and
>>&to appendoutput to existing files.
Redirection may fail under some circumstances: 1) if you have the variable noclobber set and you attempt to redirectoutput to an existing file without forcing an overwrite, 2) if you redirectoutput to a file you don't have write access to, and 3) if you redirectoutput to a directory.
Examples:
% who >names
Redirect standardoutput to a file named
names
% (pwd;ls-l) >out
Redirectoutput of both commands to a file named
out
% pwd;ls-l >out
Redirectoutput of
lscommand only to a file named
out
Input redirection can be useful, for example, if you have written a FORTRAN program which expects input from the terminal but you want it to read from a file. In the following example,
myprog, which was written to read standard input and write standardoutput, is redirected to read
myinand write
myout:
% myprog<myin>myout
You can suppress redirectedoutput and/or errors by sending it to the null device,
/dev/null. The example shows redirection of bothoutput and errors:
% who >& /dev/null
To redirect standard error andoutput to different files, you can use grouping:
% (cat myfile>myout) >& myerror
Bourne Shell Family
The Bourne shell uses a different format for redirection which includes numbers. The numbers refer to the file descriptor numbers (0 standard input, 1 standardoutput, 2 standard error). For example,2>redirects file descriptor 2, or standard error.
&nis the syntax for redirecting to a specific open file. For example
2>&1redirects 2 (standard error) to 1 (standardoutput); if 1 has been redirected to a file, 2 goes there too. Other file descriptor numbers are assigned sequentially to other open files, or can be explicitly referenced in the shell scripts. Some of the forms of redirection for the Bourne shell family are:
Character | Action |
---|---|
> | Redirect standardoutput |
2> | Redirect standard error |
2>&1 | Redirect standard error to standardoutput |
< | Redirect standard input |
| | Pipe standardoutput to another command |
>> | Append to standardoutput |
2>&1| | Pipe standardoutput and standard error to another command |
<and
>assume standard input andoutput, respectively, as the default, so the numbers 0 and 1 can be left off. The form of a command with standard input andoutput redirection is:
$ command -[options] [arguments]<input file >output file
Redirection may fail under some circumstances: 1) if you have the variable noclobber set and you attempt to redirectoutput to an existing file without forcing an overwrite, 2) if you redirectoutput to a file you don't have write access to, and 3) if you redirectoutput to a directory.
Examples:
$ who >names
Direct standardoutput to a file named
names
$ (pwd;ls-l) >out
Directoutput of both commands to a file named
out
$ pwd;ls-l >out
Directoutput of
lscommand only to a file named
out
Input redirection can be useful if you have written a program which expects input from the terminal and you want to provide it from a file. In the following example,
myprog, which was written to read standard input and write standardoutput, is redirected to read
myinand write
myout.
$ myprog<myin>myout
You can suppress redirectedoutput and/or error by sending it to the null device,
/dev/null. The example shows redirection of standard error only:
$ who2>/dev/null
To redirect standard error andoutput to different files (note that grouping is not necessary in Bourne shell):
$ cat myfile>myout2>myerror
相关文章推荐
- Standard Input and Output Redirection
- C Standard Library: Character Input and Output Functions
- C Standard Library: Direct Input and Output Functions
- C Standard Library: 1 Input and Output: <stdio.h>
- Linux - Redirection of Standard output/input
- Redirecting standard input and output
- Standard input, output, and error streams
- itk input and output
- System and device programming-Asynchronous input/output
- [20160704]Addition program that use JOptionPane for input and output
- 8 Input and Output
- Ch.7 - Input and Output
- [Reproduced]BIOS -- Basic Input and Output System
- OpenCV--File Input and Output using XML and YAML files
- input and output
- Hadoop Streaming Input and Output
- python tutorial 学习笔记(四) Input and Output, 字符串的转换等
- The Python Tutorial - Input and Output
- I/O Input and Output
- The Python Tutorial 7——Input and Output