第5章 重定向管道

对以下三个符号的理解

> < |

重定向

1、思考

1、date输出的结果,能否在下一次开机查看

不能

2、date输出的结果想保存,这还不简单?

[root@localhost ~]# touch time.txt
[root@localhost ~]# ls | grep time.txt
time.txt
[root@localhost ~]# cat time.txt 
[root@localhost ~]# date > time.txt
[root@localhost ~]# cat time.txt
Tue Sep  1 08:38:54 CST 2020

2、FD简介

file descriptors,FD,文件描述符,文件句柄

进程使用文件描述符来管理打开的文件

(程序打开一个文件时用来描述这个文件的)

软链接,可以理解为快捷方式

3、FD图示

image.png

0:stdin(standard input)

1:stdout(standard output)

2:stderr

4、FD图示

NumberChannel nameDescriptionDefault connectionUsage
0stdinStandard inputKeyboardread only
1stdoutStandard outputTerminalwrite only
2stderrStandard errorTerminalwrite only
3+filenameOther filesnoneread and/or write

5、总结:

FD(链接文件)是访问文件的标识,即链接文件。省去了冗长的绝对路径

0 是键盘只读

1,2 是终端可以理解是屏幕,

3+ 是文件,可读可写

6、示例

以vim程序示例,来观察一个进程的FD信息。

1、通过一个终端,打开一个文本

[root@izj6c4t221zhigwzt2eneoz ~]# vim example_for_FD.txt

2、通过另一个终端,查询文本程序的进程号

[root@izj6c4t221zhigwzt2eneoz ~]# ps aux | grep vim
root      110148  0.0  0.7 149356  7664 pts/0    S+   09:14   0:00 vim example_for_FD.txt
root      110150  0.0  0.2 112820  2316 pts/1    R+   09:15   0:00 grep --color=auto vim

3、在/proc目录中查看文本程序的FD

[root@izj6c4t221zhigwzt2eneoz ~]# ls /proc/

image.png

image.png

4、总结

重定向案例

1、输出重定向及综合案例

简介:输出重定向分为

正确输出:

1>等价于>

image.png

1>>等价于>>

image.png

错误输出:

2> 没有简写

2>> 没有简写

案例1:输出重定向
[root@localhost ~]# date 1> time.txt
[root@localhost ~]# date >> time.txt

image.png

>一个大于号,覆盖文件中原来的

>>两个大于号,追加

当一个程序有输出的时候,才会显示

例如mkdir命令

仅使用mkdir命令的时候,无输出,不显示

添加-v参数,将输出写入至文件/dev/pts/0

image.png

案例2:错误输出重定向

错误示范,命令正确,没有输出错误

[root@izj6c4t221zhigwzt2eneoz ~]# ls
1  bbrmod  superbench.log  tcp.sh  test  test1  time.txt
[root@izj6c4t221zhigwzt2eneoz ~]# ls /home/
admin
[root@izj6c4t221zhigwzt2eneoz ~]# ls /home/ 2> list.txt
admin
[root@izj6c4t221zhigwzt2eneoz ~]# cat list.txt

正确示范,命令错误

[root@izj6c4t221zhigwzt2eneoz ~]# ls /aaaa
ls: cannot access /aaaa: No such file or directory
[root@izj6c4t221zhigwzt2eneoz ~]# ls /aaaa 2> list.txt
[root@izj6c4t221zhigwzt2eneoz ~]# cat list.txt 
ls: cannot access /aaaa: No such file or directory
案例3:正确和错误都输入到相同位置

正确输出和错误输出都输入到同一位置

image.png

[root@izj6c4t221zhigwzt2eneoz ~]# ls /home/ /aaaa
ls: cannot access /aaaa: No such file or directory
/home/:
admin
[root@izj6c4t221zhigwzt2eneoz ~]# ls /home/ /aaaa &> list.txt
[root@izj6c4t221zhigwzt2eneoz ~]# cat list.txt
ls: cannot access /aaaa: No such file or directory
/home/:
admin

将正确或错误输出放置在不同位置,没有需求

[root@izj6c4t221zhigwzt2eneoz ~]# ls /home/ /aaaa 1> yes.txt 2> no.txt
[root@izj6c4t221zhigwzt2eneoz ~]# cat yes.txt 
/home/:
admin
[root@izj6c4t221zhigwzt2eneoz ~]# cat no.txt 
ls: cannot access /aaaa: No such file or directory

生产环境:该命令相当于将命令的输出丢入垃圾桶,不在显示器显示

[root@localhost ~]# yum install -y httpd &> /dev/null

2、输入重定向及结合案例

简介:标准输入:< 等价 0<

程序在左 < file在右

案例:输入重定向发送邮件
1、观察默认发送邮件的过程

编写邮件

[root@izj6c4t221zhigwzt2eneoz home]# mail -s "sssdfjgsdhjk" alex
jkdfghjksdghksjadgh
hjsdag
jkghlasjkhgklajsdfghlakj
ajkdfghdfjkgh
.
EOT

.代表邮件编辑已经结束。

查看邮件

[root@izj6c4t221zhigwzt2eneoz home]# su - alex
[alex@izj6c4t221zhigwzt2eneoz ~]$ mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/alex": 1 message 1 new
>N  1 root                  Tue Sep  1 20:15  21/766   "sssdfjgsdhjk"
& 

按邮件编号:1、即可看邮件。

按q退出。

2、使用重定向快速创建邮件

如果已经有了现成的邮件内容,如何快速输入邮件内容。

就可以用重定向创建邮件!!!

1、先准备一段邮件内容 vim word.txt

2、mail -s "test01" alex < word.txt

原理:利用输入重定向,把文件内容代替人为的输入。

管道

管道|

进程管道Piping

1、简介:管道命令可以将多条命令组合起来,一次性完成复杂的处理任务

2、语法: command1 | command2 | command3 | …

image.png

3、示例

cat /etc/passwd | tail -3
ps aux | grep 'sshd'

tee管道

1、简介:三通管道,即交给另一个程序处理。又保存一份副本

三通。把输出保留副本。

image.png

2、示例

tee`将`cat /etc/passwd`的输出写入文件`example.txt
[root@izj6c4t221zhigwzt2eneoz home]# cat /etc/passwd | tee example.txt | tail -1

参数传递Xargs

1、简介:cp rm一些特殊命令就是不服其他命令,加上xargs就服了。

转换。把输出和输入进行格式转换。

2、示例

1、环境准备,准备一些文件。

touch /home/file{1..5}
ls /home

2、接到消息,部分文件需要删除,这些文件的名字在文本当中。

[root@localhost ~]# vim files.txt

/home/file1

/home/file3

/home/file5

3、使用管道

[root@localhost ~]# cat files.txt | rm -rvf

失败

4、貌似之前的不行。下面加上xargs

[root@localhost ~]# cat files.txt | xargs rm -rvf

removed ‘/home/file1’

removed ‘/home/file3’

removed ‘/home/file5’

[root@localhost ~]# ls /home/

通过xargs成功连接rm命令

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值