Linux~~~重定向管道- 2020.11.24

重定向管道

重定向

date的输出结果,可以保存在文档中:[root@localhost ~]# date > date.txt
查证:
[root@localhost ~]# cat date.txt
2020年 11月 24日 星期二 16:52:56 CST

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

FD简介:file description,FD,文件描述符
进程使用文件描述符来管理打开的文件–链接文件 由0~255的数字链接文件组成
数字:0~255
在这里插入图片描述
stdout:标准输出
stdin:标准输入
stderr:标准错误
在这里插入图片描述
FD是访问文件的标识,即链接文件:
标准输入是文件描述符0。它是命令的输入,缺省是键盘,也可以是文件或其他命令的输出。链接键盘
标准输出是文件描述符1。它是命令的输出,缺省是屏幕,也可以是文件。链接屏幕/终端
标准错误是文件描述符2。这是命令错误的输出,缺省是屏幕,同样也可以是文件。链接屏幕/终端
3+ 是文件,可读可写(3之后的都是文件)

举例:

1、终端一:查看当前终端信息+打开文本
[root@localhost ~]# tty
/dev/pts/1
[root@localhost ~]# vim 1.txt
2、终端二:查询1.txt进程号
[root@localhost ~]# ps aux | grep vim
root 8357 0.3 0.2 149700 5540 pts/1 S+ 17:23 0:00 vim 1.txt
root 8362 0.0 0.0 112728 972 pts/0 R+ 17:23 0:00 grep --color=auto vim

在/proc目录中查看文本程序FD:
[root@localhost ~]# cd /proc/8357
[root@localhost 8357]# ls -l fd
总用量 0
lrwx------. 1 root root 64 11月 24 17:26 0 -> /dev/pts/1 标准输入
lrwx------. 1 root root 64 11月 24 17:26 1 -> /dev/pts/1 标准输出
lrwx------. 1 root root 64 11月 24 17:23 2 -> /dev/pts/1 标准错误输出
lrwx------. 1 root root 64 11月 24 17:26 4 -> /root/.1.txt.swp 常规文件
上面的0124就是FD,程序通过描述符访问文件,可以是常规文件,也可以是设备文件

输出重定向及综合案例

FD:1、2
输出重定向分为正确输出和错误输出:
正确输出:
1>等价于>
[root@localhost ~]# cat /etc/passwd > date.txt
查证:[root@localhost ~]# cat date.txt | head -3
root:X:0:0:root:/root:/bin/bash
bin:X:1:1:bin:/bin:/sbin/nologin
daemon:X:2:2:daemon:/sbin:/sbin/nologin
[直接覆盖文档内的内容]
1>>等价于>>
[root@localhost ~]# date >> date.txt
查证:
[root@localhost ~]# cat date.txt | tail -2
user02:X:1009:1011::/home/user02:/bin/bash
2020年 11月 24日 星期二 19:17:56 CST
[在原本内容基础上追加,不覆盖]

错误输出:
2>(没有简写)
[root@localhost ~]# datt 2> date.txt
查证:
[root@localhost ~]# cat date.txt
bash: datt: 未找到命令…
[覆盖]
2>>(没有简写)
[root@localhost ~]# dhudywh 2>> date.txt
查证:
[root@localhost ~]# cat date.txt
bash: datt: 未找到命令…
bash: dhudywh: 未找到命令…
[追加]

举例1:输出定向
[root@localhost ~]# date 1> date.txt
[root@localhost ~]# cat date.txt
2020年 11月 24日 星期二 19:26:19 CST

[root@localhost ~]# date >> date.txt
[root@localhost ~]# cat date.txt
2020年 11月 24日 星期二 19:26:19 CST
2020年 11月 24日 星期二 19:26:41 CST

[root@localhost ~]# mkdir -v dir33 >> date.txt
[root@localhost ~]# cat date.txt
2020年 11月 24日 星期二 19:26:19 CST
2020年 11月 24日 星期二 19:26:41 CST
mkdir: 已创建目录 “dir33”

【mkdir 不可用 因为没有输出 加上-v可用 -v:显示创建过程(程序本身需要输出)】

举例2:错误输出定向
当某条命令产生错误时,才会有错误输出,存入文件中

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

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

可各自分类收集:
[root@localhost ~]# ls /home/ /hhhhhhh 1> yes.txt 2> no.txt
查证:
[root@localhost ~]# cat yes.txt no.txt
/home/:
file2
file4
hang
li
lijinhang
lll
test.txt
user01
user02
user09
user100
user200
user300
ls: 无法访问/hhhhhhh: 没有那个文件或目录

可用&(and)都收入一个文件:
[root@localhost ~]# ls /home/ /hhhhhhh &> date.txt
[root@localhost ~]# cat date.txt
ls: 无法访问/hhhhhhh: 没有那个文件或目录
/home/:
file2
file4
hang
li
lijinhang
lll
test.txt
user01
user02
user09
user100
user200

可收入垃圾桶中:
[root@localhost ~]# ls /home/ /hhhhhhh &> /dev/null

输入定向及结合案例

标准输入:<等价于0<
命令 < 文件 将指定文件作为命令的输入设备

[root@localhost ~]# cat /etc/passwd | head -3
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin

[root@localhost ~]# cat < /etc/passwd | head -3
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin
第一行代表是以键盘作为输入设备,而第二行代码是以 /etc/passwd 文件作为输入设备。

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

1、观察默认发送邮件的过程
编写邮件:
mail: 电子邮件
-s :标题
abcdefg:标题内容
user01 :邮件接收人
. :结束符号

[root@localhost ~]# mail -s “abcdefg” user01
asdf
asd
zc
fg
.
EOT

查看邮件:
[root@localhost ~]# su - user01
上一次登录:二 11月 24 14:08:10 CST 2020pts/0 上
[user01@localhost ~]$ mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
“/var/spool/mail/user01”: 4 messages 2 new
1 root Tue Nov 24 14:06 24/652 “abcdefghijk”
2 root Tue Nov 24 14:07 35/1553 “555”
>N 3 root Tue Nov 24 19:40 21/631 “abcdefg”
N 4 root Tue Nov 24 19:43 21/622 “abcdefg”
&
按邮件编号:1、2、3、4任意即可查看邮件
按q退出

2、使用重定向快速创建邮件
如果已经有了现成的邮件内容呢,如何快速输入邮件内容。
就可以用重定向创建邮件!!!
先准备一段邮件内容 date.txt
[root@localhost ~]# mail -s “12345” user01 < date.txt
原理:利用输入重定向,把文件内容代替人为的输入。
查证:略(同上)

管道

进程管道:
管道命令可以将多条命令组合起来,一次性完成复杂的处理任务
语法:command1 | command2 | command3 |…
在这里插入图片描述
指令1的标准输出作为指令2 的标准输入

举例:
[root@localhost ~]# cat /etc/passwd | tail -2
user300❌1008:1009::/home/user300:/bin/bash
user02❌1009:1011::/home/user02:/bin/bash

[root@localhost ~]# ps aux | grep ‘sshd’
root 1208 0.0 0.2 112920 4312 ? Ss 19:03 0:00 /usr/sbin/sshd -D
root 4055 0.0 0.0 112728 972 pts/0 R+ 19:56 0:00 grep --color=auto sshd

tee管道:
三通管道,即交给另一个程序处理,又保存一份副本
在这里插入图片描述
[root@localhost ~]# cat /etc/passwd |tee 78.txt |tail -1
user02❌1009:1011::/home/user02:/bin/bash
[将/etc/passwd的内容保存在78.txt中]

[root@localhost ~]# cat /etc/passwd |grep x |tee 78.txt|tail -2
user300❌1008:1009::/home/user300:/bin/bash
user02❌1009:1011::/home/user02:/bin/bash

[将grep x 的内容保存在78.txt中]

参数传递Xargs:
cp rm一些特殊命令就是不服其他程序。
1、环境准备,准备一些文件
[root@localhost ~]# touch /home/file{1…5}
[root@localhost ~]# cd /home
[root@localhost home]# ls
file1 file3 file5 li lll user01 user09 user200
file2 file4 hang lijinhang test.txt user02 user100 user300

2、需要删除部分文件
[root@localhost home]# vim files.txt
输入:
/home/file2
/home/file3
/home/file4
/home/file5

3、使用管道|
[root@localhost home]# cat files.txt | rm -rf
[root@localhost home]# ls
file1 file3 file5 hang lijinhang test.txt user02 user100 user300
file2 file4 files.txt li lll user01 user09 user200
[失败]

4、加上xargs
[root@localhost home]# cat files.txt |xargs rm -rf
[root@localhost home]# ls
file1 hang lijinhang test.txt user02 user100 user300
files.txt li lll user01 user09 user200
[删除成功]
但files.txt中文件还存在(不影响)

[root@localhost home]# cat files.txt |xargs rm -rvf
已删除"/home/file1"
(v 可显示删除过程)

[通过|xargs成功连接rm命令]

命令<文件1>文件2
将文件1作为命令的输入设备,该命令的执行结果输出到文件2中
[root@localhost ~]# cat < /etc/passwd > date.txt
查证:
[root@localhost ~]# cat date.txt | tail -3
user200❌1007:1008::/home/user200:/bin/bash
user300❌1008:1009::/home/user300:/bin/bash
user02❌1009:1011::/home/user02:/bin/bash

date.txt输出了和/etc/passwd文件内容相同的数据

ln -s 链接文件:
[root@localhost ~]# touch 1.txt
[root@localhost ~]# ln -s 1.txt 1.ttt

alias :别名
[root@localhost ~]# alias ll
alias ll=‘ls -l --color=auto’

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值