Linux学习之路

IO重定向及管道

2017-08-23  本文已影响0人  香吉矢

tr,tee

程序的IO

简单的说程序由指令(命令)和数据(操作对象)组成,在linux上一切皆文件,程序操作对象(数据)来自系统上的各种文件即程序的输入,由程序处理之后输出的结果即程序的输出

IO重定向

不使用系统默认的标准输入输出,而是重新指定,即输入重定向,输出重定向,错误输出重定向,双重输出重定向(一次单独地送到不同的地方)。IO重定向其实就是让已创建的FD指向其它的文件(修改其链接的文件)。输入输出数据流的位置用FD标识

输出重定向
输入重定向

<,会经常和tr命令一起使用。介绍下tr命令的用法

管道

链接程序,实现前一个命令的输出直接定向到后一个程序并当作输入数据流(Linux的哲学思想)
只会将最后一个命令的执行结果,作为标准输出,前面的命令的输出都会作为下一个命令的输入,不会在屏幕上显示了。
COMMAND1 | COMMAND2 | COMMAND3 |……
管道符与tee命令结合使用,将前一个命令的标准输出保存一份,然后继续通过管道符送到下一个命令,实现命令分方向发送。tee介绍如下:

相关演示

[root@localhost mytest]# touch fedora   \\创建一个空文件
[root@localhost mytest]# ll
总用量 0
-rw-r--r--. 1 root root 0 8月 13 22:50 fedora
[root@localhost mytest]# echo diyihang > fedora   \\将echo回显的内容重定向到fedora
[root@localhost mytest]# cat fedora   
diyihang
[root@localhost mytest]# echo dierhang > fedora   \\覆盖重定向,第一次的内容被覆盖
[root@localhost mytest]# cat fedora
dierhang
[root@localhost mytest]# echo desanhang >> fedora   \\追加重定向
[root@localhost mytest]# cat fedora
dierhang
desanhang
[root@localhost mytest]# set -C   \\禁止重定向覆盖已存在的文件,即使文件为空的。
[root@localhost mytest]# echo disihang > fedora   
-bash: fedora: 无法覆盖已存在的文件
[root@localhost mytest]# echo disihang >> fedora   \\只可以追加
[root@localhost mytest]# cat fedora
dierhang
desanhang
disihang
[root@localhost mytest]# echo dierhang >| fedora   \\强制覆盖
[root@localhost mytest]# cat fedora
dierhang
[root@localhost mytest]# set +C   \\取消进制覆盖重定向
[root@localhost mytest]# echo diyihang > fedora
[root@localhost mytest]# cat fedora
diyihang
[root@localhost mytest]# > fedora
[root@localhost mytest]# cat fedora
[root@localhost mytest]# echo dierhang > suse   \\重定向的文件不存在,会自动创建此文件
[root@localhost mytest]# ll
总用量 8
-rw-r--r--. 1 root root 9 8月 13 22:54 fedora
-rw-r--r--. 1 root root 9 8月 13 22:54 suse
[root@localhost mytest]# cat suse
dierhang
[root@localhost mytest]# > centos   \\重定向到新文件,什么也没有,即创建空文件,相当于touch
[root@localhost mytest]# ll
总用量 8
-rw-r--r--. 1 root root 0 8月 13 22:54 centos
-rw-r--r--. 1 root root 9 8月 13 22:54 fedora
-rw-r--r--. 1 root root 9 8月 13 22:54 suse
[root@localhost mytest]# ll centos1
ls: 无法访问centos1: 没有那个文件或目录
[root@localhost mytest]# ll centos1 2> ubuntu
[root@localhost mytest]# cat ubuntu
ls: 无法访问centos1: 没有那个文件或目录
[root@localhost mytest]# ll centos2 2>> ubuntu
[root@localhost mytest]# cat ubuntu
ls: 无法访问centos1: 没有那个文件或目录
ls: 无法访问centos2: 没有那个文件或目录
[root@localhost mytest]# cat fedora
diyihang
[root@localhost mytest]# cat fedora &> suse   \\合并重定向
[root@localhost mytest]# cat suse
diyihang
[root@localhost mytest]# cat fedora1 &>> suse
[root@localhost mytest]# cat suse
diyihang
cat: fedora1: 没有那个文件或目录
[root@localhost mytest]# cat fedora1
cat: fedora1: 没有那个文件或目录
[root@localhost mytest]# cat fedora1 > suse 2>&1
[root@localhost mytest]# cat suse
cat: fedora1: 没有那个文件或目录
[root@localhost mytest]# cat fedora > suse 2>&1
[root@localhost mytest]# cat suse
diyihang
[root@localhost mytest]# cat fedora1 >> suse 2>&1
[root@localhost mytest]# cat suse
diyihang
cat: fedora1: 没有那个文件或目录
[root@localhost mytest]# ls /tmp/mytest/
centos fedora suse ubuntu
[root@localhost mytest]# ls /tmp/mytest1 &> /dev/null   \\可以通过合并重定向到null文件,根据命令执行的状态值,判断是否有该目录。
[root@localhost mytest]# echo $?   \\返回值为2,命令执行失败,所以没有mytest1目录
2
[root@localhost mytest]# ls /tmp/mytest &> /dev/null
[root@localhost mytest]# echo $?   \\返回值为0,命令执行成功,所以有mytest目录
0
[root@localhost mytest]# cat fedora
diyihang
dierhang
disanhang
disiheng
5diwuhang6
di6hang
[root@localhost mytest]# tr [a-z] [A-Z] < fedora   \\通过符号"<"将文件的内容送给命令tr处理,即将所有小写字母,变为大写。"<"后边为已经存在的文件
DIYIHANG
DIERHANG
DISANHANG
DISIHENG
5DIWUHANG6
DI6HANG
[root@localhost mytest]# cat fedora
diyihang
dierhang
disanhang
disiheng
5diwuhang6
di6hang
[root@localhost mytest]# tr -d "hang" < fedora   \\删除fedora中出现的字符串hang,非单词。
diyi
dier
dis
disie
5diwu6
di6
[root@localhost mytest]# tr -d "[[:digit:]]" < fedora
diyihang
dierhang
disanhang
disiheng
diwuhang
dihang
[root@localhost mytest]# tr -d "[0-9]" < fedora
diyihang
dierhang
disanhang
disiheng
diwuhang
dihang
[root@localhost mytest]# ll
总用量 12
-rw-r--r--. 1 root root 0 8月 13 22:54 centos
-rw-r--r--. 1 root root 56 8月 13 23:33 fedora
-rw-r--r--. 1 root root 51 8月 13 23:26 suse
-rw-r--r--. 1 root root 106 8月 13 22:57 ubuntu
[root@localhost mytest]# ll | head -2   \\通过ll命令的输出作为输入交给head -2处理,后者处理后输出到屏幕
总用量 12
-rw-r--r--. 1 root root 0 8月 13 22:54 centos
[root@localhost mytest]# ll | tee suse | head -2   \\将ll命令输出的内容,复制一份到suse,然后继续交由下一个命令处理
总用量 12
-rw-r--r--. 1 root root 0 8月 13 22:54 centos
[root@localhost mytest]# cat suse
总用量 12
-rw-r--r--. 1 root root 0 8月 13 22:54 centos
-rw-r--r--. 1 root root 56 8月 13 23:33 fedora
-rw-r--r--. 1 root root 51 8月 13 23:26 suse
-rw-r--r--. 1 root root 106 8月 13 22:57 ubuntu
[root@localhost mytest]# ls | tee suse | grep centos
centos
[root@localhost mytest]# cat suse
centos
fedora
suse
ubuntu
[root@localhost mytest]# ls fedora | tee -a suse | tr [a-z] [A-Z]    \\此处tr处理的字符是前一个命令的输出通过管道符做为输入交给其处理,tee -a追加到文件。
FEDORA
[root@localhost mytest]# cat suse
centos
fedora
suse
ubuntu
fedora
[root@localhost mytest]#

上一篇 下一篇

猜你喜欢

热点阅读