I/O redirection
时间:2005-11-14 来源:rickyweiwei
Most Linux commands read input, such as a file or another attribute for the command, and write output. By default, input is being given with the keyboard, and output is displayed on your screen. Your keyboard is your "standard input" (stdin) device, and the screen or a particular terminal window is the "standard output" (stdout) device.
输入:过滤器的数据来源 标准输入stdin(0) default is keyboard
输出:守滤器的数据去向 标准输出stdout(1) default is terminal screen
错误输出:报错讯息与单独由 stderr(2) default is terminal screen
重定向:标准输入、出及错误输出均可用特定符号改变数据来去方向。
当一个shell命令行打开时通常会自动打开三个标准文件:stdin stdout stderr
输入重定向:
cat >file << a ......... a其中字符a作为起始与终止界定符。而<<让系统将一次的键盘的全部输入送入文档
输出重定向:
覆盖与追加 > 原文件内容被覆盖 >>输出追加到文件
错误重定向:
系统错误输出使用的I/O管道不同
缺省输出到终端屏幕 使用2>将报错讯息重定向到一个文件,2>>将报错信息追加到一个文件
/dev/null 俗称黑洞设备
example:
find / -perm -2 2>error >results 将操作正确、错误的输入被单独送往不同的地方
find / -perm +6000 &>file &将所有输出都送向同一个地方。2>&1 将1、2全部输出到同一个地方。
输出重定向只将执行正确后的结果送到标准输出,错误输出输出重定向不进行输出。
管道
ls -al | more
cat file2 文件复制
cat file.* >file 文件合并
注意:
Don't overwrite!
Be careful not to overwrite existing (important) files when redirecting output. Many shells, including Bash, have a built-in feature to protect you from that risk: noclobber. See the Info pages for more information. In Bash, you would want to add the set -o noclobber command to your .bashrc configuration file in order to prevent accidental overwriting of files.(保护意外的文件覆盖)
补充1:三个最常用的shell特殊变量有:ignoreeof、noclobber及noglob。
ignoreeof
ignoreeof变量用来禁止使用ctrl+d来退出shell(ctrl+d不仅用来退出shell,而且可以终止用户直接输往标准输出上的输入。该操作经常在一些shell实用命令中使用,例如实用命令cat。在这些实用程序操作中,非常容易误操作而意外地退出shell。ignoreeof特殊变量正是用来防止这种意外的退出。例如:
$ set –o ignoreeof
之后,用户只能用logout或exit命令退出shell。
noclobber
noclobber变量可以在重定向输出时保护已存在的文件,防止被意外地覆盖。在下例中,用户设置noclobber为有效,在重定向时,用户试图去覆盖已经存在的文件myfile,此时系统将返回一个错误信息。
[例]
$ set –o noclobber
$ cat preface>myfile
bash: myfile: cannot overwrite existing file
$noglob
设置noglob变量后,shell将不扩展文件名中一些特殊的字符或字符串。如字符*、?、[ ]等将不再作为通配符。如果用户希望列出结尾为?的文件名answer?,可通过如下步骤:首先,用户使noglob变量为无效,然后再列出文件名。可以看到,目前命令行上的问号?被认为是文件名中的一个字符,而不再被看作通配符。
$ set –o noglob
EXAMPLE2:
Redirecting "nothing" to an existing file is equal to emptying the file:
# ls -l list
-rw-rw-r-- 1 nancy nancy 117 Apr 2 18:09 list
# > list
# ls -l list
-rw-rw-r-- 1 nancy nancy 0 Apr 4 12:01 list
This process is called truncating.
The same redirection to an nonexistent file will create a new empty file with the given name:
# ls -l newlist
ls: newlist: No such file or directory
# > newlist
# ls -l newlist
-rw-rw-r-- 1 nancy nancy 0 Apr 4 12:05 newlist
3、 Some examples using piping of commands:
To find a word within some text, display all lines matching "pattern1", and exclude lines also matching "pattern2" from being displayed:
grep pattern1 file | grep -v pattern2
4、The following command lists all commands that you can issue to examine another file when using less:
# less --help | grep -i examine
:e [file] Examine a new file.
:n * Examine the (N-th) next file from the command line.
:p * Examine the (N-th) previous file from the command line.
:x * Examine the first (or N-th) file from the command line.
The -i option is used for case-insensitive searches - remember that UNIX systems are very case-sensitive.(大小写敏感)
Advanced redirection features
There are three types of I/O, which each have their own identifier, called a file descriptor:
standard input: 0
standard output: 1
standard error: 2
In the following descriptions, if the file descriptor number is omitted, and the first character of the redirection operator is <, the redirection refers to the standard input (file descriptor 0). If the first character of the redirection operator is >, the redirection refers to the standard output (file descriptor 1).
5、Some practical examples will make this more clear:
ls > dirlist 2>&1
will direct both standard output and standard error to the file dirlist, while the command
ls 2>&1 > dirlist
will only direct standard output to dirlist. This can be a useful option for programmers.
6、If your process generates a lot of errors, this is a way to thoroughly examine them:
command 2>&1 | less (同时输出标准信息与错误信息)
This is often used when creating new software using the make command, such as in:
andy:~/newsoft> make all 2>&1 | less
--output ommitted--
7、Separating standard output from standard error(将错误输出与标准输出分别输出到不同的地方)
Constructs like these are often used by programmers, so that output is displayed in one terminal window, and errors in another. Find out which pseudo terminal you are using issuing the tty command first:
a> make all 2> /dev/pts/7
8、 Writing to output and files simultaneously(输出信息并保存相应结果)
You can use the tee command to copy input to standard output and one or more output files in one move. Using the -a option to tee results in appending input to the file(s). This command is useful if you want to both see and save output. The > and >> operators do not allow to perform both actions simultaneously.
This tool is usually called on through a pipe (|), as demonstrated in the example below:
# date | tee file1 file2
Thu Jun 10 11:10:34 CEST 2004
# cat file1
Thu Jun 10 11:10:34 CEST 2004
# cat file2
Thu Jun 10 11:10:34 CEST 2004
# uptime | tee -a file2
11:10:51 up 21 days, 21:21, 57 users, load average: 0.04, 0.16, 0.26
# cat file2
Thu Jun 10 11:10:34 CEST 2004
11:10:51 up 21 days, 21:21, 57 users, load average: 0.04, 0.16, 0.26
9、与sort一起对结果排序