15 标准I/O:重定向和管道
15.1 Unix设计准则
一个程序只做一件事,但必须出色地完成这件事。
Small is beautiful Less is more.
15.2 Unix新设计准则
除非程序无法更小,否则小就是完美的。
15.3 标准输入、标准输出和标准错误
To save the output of a program, write standard output to file.
15.4 重定向标准输出
sort > names 将sort的输出写入到names的文件中。
sort >> names 将sort的输出追加到names的文件的尾部。
15.5 放止文件被重定向替换或创建
set -o(+o) noclobber 置位(复位) B-Shell
set(unset) noclobber 置位(复位) C-Shell
sort >| names 忽略noclobber选项,强行替换文件 B-Shell
sort >! names 忽略noclobber选项,强行替换文件 C-Shell
在设置了noclobber的情况下,对于:sort >> names,Bourne Shell依然可以追加,而C-Shell和Tcsh则不可以
??1:经试验,我的Ubuntu的C-shell和Tcsh在设置了noclobber选项后,sort>>names依然可以被执行,为什么?
15.6 重定向标准输入
sort < /etc/passwd 对passwd文件进行排序
sort < /etc/passed > report 从passwd文件读取数据,排序之后,讲述出写入到report文件中。
15.7 文件描述符、Bourne Shell家族重定向标准错误
calculate 8> results 假设一个程序calculate被设计为输出写入到文件描述符为8的文件中,使用此命令运行改程序,能将输出重定向到results文件。
Unix为每个进程提供3个预定义的文件描述符,0代表标准输入,1代表标准输出,2代表标准错误
command 0< inputfile
command 1> outputfile
command 2> errorfile
sort 0< rawdata 1> results 2> errors 此命令于下面的等价,只不过下面的标准错误的“2”不可省略
sort < rawdata >results 2> errros
15.8 子shell
子shell创建或修改了环境变量,改变并不会影响原始的shell。
在子shell中运行date命令,可是使用:
(date)
两种使用子shell的方法:
1.
bash
cd ../spreadsheets
export DATA=statistics
calculate
2.
( cd ../spreadsheets;export DATA=statistics;calculate ) 可防止cd命令影响到当前的shell
15.9 在C-shell家族中重定向标准错误
command >& outputfile
使标准错误和标准输出的重定向相互分离:
(command > outputfile) >& errorfile
(command >> outputfile) >>& errorfile
15.10 组合标准输出和标准错误
C-Shell:
sort >& output
sort >>& output
B-Shell:
command x> outputfile y>&x x,y:文件描述符
例:
sort 1> output 2>&1
sort > output 2>&1
sort 2>&1 > output 标准输出在被重定向之前发给了shell,当shell处理2>&1时,标准输出仍然发送给显示屏,标准错误最终也被重定向到显示屏。
sort 1>> output 2>&1
sort 2> output 1>&2
同时重定向标准输出和标准错误(Bash):
sort >& output
sout &> output
15.11 抛弃输出
/dev/null文件:位桶(bit bucket),读"slash-dev-slash-null"或"dev-null",发送给它的任何东西都会永远消失。
标准输出重定向到到/dev/null:
command > /dev/null
希望查看正常输出,但是不希望查看错误消息:
command 2> /dev/null (Bash,Korn)
抛弃所有输出和错误:
command >& /dev/null (C-Shell)
command > /dev/null 2>&1 (Bash,Korn)
希望抛弃标准错误,不希望抛弃标准输出:
(command > /dev/tty) >& /dev/null (C-Shell)a
15.12 重定向:小结和体验
Bourne shell:
元字符 动作
< 重定向标准输入(同0<)
> 重定向标准输出(同1>)
>| 重定向标准输出:强制重写
>> 追加标准输出(同1>>)
2> 重定向标准错误
2>> 追加标准错误
2&>1 将标准错误重定向到标准输出
>&或&> 重定向标准输出+标准错误(只适用于Bash)
| 将标准输出通过管道传送给另一条命令
2>&1| 将标准输出+标准错误通过管道传送给另一条命令
如果设置了noclobber选项,则可以使用>|强制重写
C-Shell:
元字符 动作
< 重定向标准输入
> 重定向标准输出
>! 重定向标准输出:强制重写
>& 重定向标准输出+标准错误
>&! 重定向标准输出+标准错误;强制重写
>>& 追加向标准输出+标准错误
>>&! 追加向标准输出+标准错误;强制文件创建
| 将标准输出通过管道传送给另一条命令
|& 将标准输出+标准错误通过管道传送给另一条命令
如果设置了noclobber变量,则可以使用一个!字符强制进行重写或文件创建。
15.13 管道线
管道(pipe):两个程序之间的连接即使管道。
管道线(pipeline):命令序列本身。
cat file1 file2 file3 > timpfile1 #combine files
grep Harley < tempfile1 > tempfile2 #extract lines
wc -l < tempfile2 #count lines
rm tempfile1 tempfile2 #delete temp files
将标准输出和标准错误一起发送给另一个程序:
command1 2>& | command2 Bourne shell
command1 |& command2 C-Shell
15.14 管道线分流:tee
有时候,可能希望将程序的输出同时发送到两个地方。
tee命令语法:
tee [-a] file ...
-a(append)
例;
cat names1 names2 | tee masterlist | grep Harley cat的输出保存在masterlist文件中,同时cat的输出传送给grep.
cat names1 names2 | tee d1 d2 | grep Harley tee将cat的输出复制到两个文件d1,d2中.
cat names1 names2 | tee -a backup | grep Harley cat的输出将追加到backup文件的结尾.
希望查看命令的输出并且同时巴蜀出保存到文件中: command | tee file
例:
who | tee status
hwo | tee status | less
15.15 管道的重要性
“To put my strongest into a nutshell:
We should havae some ways of connecting programs like a garden hose----screw in another segment when it becomes necessary to massage data in another way...”
Dong McIlroy 1964-10-11
15.16 条件执行
And: command1 && command2
Or: command1 || command2
如果需要终止一个正在运行的管道线,只需按下发送intr信号.