云计算基础:第五章 Linux重定向管道

第五章重定向管道

首先我们思考一个问题,date输出的结果,能否在下一次开机查看?

当然不能! 所以我们要用 date > date.txt将date显示的信息保存在txt文件里。

1 重定向

  • 标准输入、标准输出、标准错误

FD简介:file descriptors ,FD,文件描述符进程使用文件描述符来管理打开的文件,即链接文件。

在这里插入图片描述

在这里插入图片描述

FD是访问文件的标识,即链接文件,其中 0是键盘只读,1,2是终端可以理解是屏幕,3+是文件,可读可写。

示例:

#通过我们非常熟悉的VIM程序。来观察一个进程的FD信息。
#1.通过一个终端,打开一个文本。
[root@localhost ~]# vim 1.txt
#2.通过另一个终端,查询文本程序的进程号
[root@localhost ~]# ps aux| grep vi
root        732  0.0  0.6  99684  6064 ?        Ss   17:54   0:00 /usr/bin/VGAuthService -s
root     109208  0.0  0.1 126352  1680 pts/0    S+   19:44   0:00 vi 1.txt
root     110164  0.0  0.0 112732   972 pts/2    S+   19:44   0:00 grep --color=auto vi
#3.在/proc目录中查看文本程序的FD,通常在 /proc/PID/fd  就能看到文件的FD调用情况。
[root@localhost ~]# ls /proc/109208/fd
0  1  2  4
[root@localhost ~]# ll /proc/109208/fd
总用量 0
lrwx------. 1 root root 64 7月  30 19:47 0 -> /dev/pts/0  //标准输入
lrwx------. 1 root root 64 7月  30 19:47 1 -> /dev/pts/0   //标准输出
lrwx------. 1 root root 64 7月  30 19:44 2 -> /dev/pts/0   //标准错误输出
lrwx------. 1 root root 64 7月  30 19:47 4 -> /root/.1.txt.swp // 常规文件
#4 总结 看到的0124就是FD,程序通过描述访问文件,可以是常规文件,也可以是设备文件。
  • 重定向案例

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

    简介:FD 1和2

    *输出重定向:*正常输出 1>> 等价于>> 追加 错误输出 2> 没有简写 2>> 没有简写 1>等价> 覆盖

    案例1,输出重定向

    [root@localhost ~]# date 1> date.txt
    [root@localhost ~]# cat date.txt 
    2020年 07月 30日 星期四 20:10:50 CST
    [root@localhost ~]# date 1>> date.txt
    [root@localhost ~]# cat date.txt 
    2020年 07月 30日 星期四 20:10:50 CST
    2020年 07月 30日 星期四 20:11:07 CST
    

    在这里插入图片描述

    案例2:错误输出重定向

    错误示范:

    [root@localhost ~]# ls /home/ 2> list.txt
    #观察list.txt文件中有没有内容? 因为没有错误信息所以没有
    

    正确示范:

    [root@localhost ~]#  ls  /aaaaaaaaa 2>  list.txt 
    [root@localhost ~]# cat list.txt 
    ls: 无法访问/aaaaaaaaa: 没有那个文件或目录
    

在这里插入图片描述

案例3,正确和错误都输入到相同位置

[root@localhost ~]# ls /home/ /aaaaaaaaa &>list.txt
[root@localhost ~]# cat list.txt 
ls: 无法访问/aaaaaaaaa: 没有那个文件或目录
/home/:
kk4real

在这里插入图片描述

2.输入重定向及综合案例

  • 标准输入: <等价 0<

  • 案例:输入重定向发送邮件

    1.观察默认发送邮件的过程

    #编写邮件
    [root@localhost ~]# mail -s "kk4real" alice
    1234
    2222
    3333
       
    .
    EOT
    #点代表邮件编辑已结束。 mail-电子邮件 -s-标题 1234-标题内容 kk4real邮件接收人 .-结束符号 
    #查看邮件
    [root@localhost ~]# su - kk4real
    [kk4real@localhost ~]$ mail
    Heirloom Mail version 12.5 7/5/10.  Type ? for help.
    "/var/spool/mail/kk4real": 1 message 1 new
    >N  1 root                  Thu Jul 30 20:26  21/617   "1234"
    & 1
    Message  1:
    From [email protected]  Thu Jul 30 20:26:28 2020
    Return-Path: <[email protected]>
    X-Original-To: kk4real
    Delivered-To: [email protected]
    Date: Thu, 30 Jul 2020 20:26:28 +0800
    To: [email protected]
    Subject: 1234
    User-Agent: Heirloom mailx 12.5 7/5/10
    Content-Type: text/plain; charset=us-ascii
    From: [email protected] (root)
    Status: R
    
    1234
    2222
    3333
    
    
    & 
    #按邮件编号:1即可查看邮件 按q退出
    

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

    #如果已经有了现成的邮件内容呢,如何快速输入邮件内容。就可以用重定向创建邮件!!!
    [kk4real@localhost ~]$ vi word.txt
    [kk4real@localhost ~]$ mail -s "subject01"  root < word.txt 
    # 原理:利用输入重定向,把文件内容代替人为的输入。
    

2 管道

管道 |

  • 进程管道Piping

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

语法: command1 | command2 |command3 |…

图片

指令1的标准输出 作为指令2的标准输入

  • tee管道

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

在这里插入图片描述

使用tee在运行同时还可以看到命令的信息 加在想查看的命令|的后面

案例:

#使用tee在运行同时还可以看到命令的信息 加在想查看的命令|的后面
[root@localhost ~]# cat /etc/passwd|tail -1
kk4real:x:1000:1000:kk4real:/home/kk4real:/bin/bash
[root@localhost ~]# cat /etc/passwd|tee 888.txt|tail -1
kk4real:x:1000:1000:kk4real:/home/kk4real:/bin/bash
[root@localhost ~]# cat 888.txt  
#此时888.txt里面是全部的命令1 (cat)处理的结果


  • 参数传递Xargs

cp rm一些特殊命令就是不服其他程序。

案例1:

#环境准备 准备一些文件
[root@localhost ~]# touch /home/file{1..5}
[root@localhost ~]# ls /home/
file1  file2  file3  file4  file5  kk4real
#接到消息,部分文件需要删除 
[root@localhost ~]# vim files.txt 
/home/file1
/home/file3
/home/file5
#使用管道
[root@localhost ~]# cat files.txt|rm -rvf  //v代表显示过程
[root@localhost ~]# ls /home/
file1  file2  file3  file4  file5  kk4real
#失败
#貌似之前的不行,下面加上xargs
[root@localhost ~]# cat files.txt |xargs rm -rvf
已删除"/home/file1"
已删除"/home/file3"
已删除"/home/file5"
[root@localhost ~]# ls /home/
file2  file4  kk4real
#通过|xargs成功连接rm命令

猜你喜欢

转载自blog.csdn.net/weixin_44898311/article/details/107700418