Linux~~~ 2020.11.24
重定向管道
重定向
date的输出结果,可以保存在文档中:[[email protected] ~]# date > date.txt
查证:
[[email protected] ~]# cat date.txt
2020年 11月 24日 星期二 16:52:56 CST
标准输入、标准输出、标准错误
FD简介:file description,FD,文件描述符
进程使用文件描述符来管理打开的文件–链接文件
数字:0~255
stdout:标准输出
stdin:标准输入
stderr:标准错误
FD是访问文件的标识,即链接文件:
标准输入是文件描述符0。它是命令的输入,缺省是键盘,也可以是文件或其他命令的输出。
标准输出是文件描述符1。它是命令的输出,缺省是屏幕,也可以是文件。
标准错误是文件描述符2。这是命令错误的输出,缺省是屏幕,同样也可以是文件。
3+ 是文件,可读可写(3之后的都是文件)
举例:
1、终端一:查看当前终端信息+打开文本
[[email protected] ~]# tty
/dev/pts/1
[[email protected] ~]# vim 1.txt
2、终端二:查询1.txt进程号
[[email protected]calhost ~]# 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:
[[email protected] ~]# cd /proc/8357
[[email protected] 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>等价于>
[[email protected] ~]# cat /etc/passwd > date.txt
查证:[[email protected] ~]# 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>>等价于>>
[[email protected] ~]# date >> date.txt
查证:
[[email protected] ~]# cat date.txt | tail -2
user02:X:1009:1011::/home/user02:/bin/bash
2020年 11月 24日 星期二 19:17:56 CST
[在原本内容基础上追加,不覆盖]
错误输出:
2>(没有简写)
[[email protected] ~]# datt 2> date.txt
查证:
[[email protected] ~]# cat date.txt
bash: datt: 未找到命令…
[覆盖]
2>>(没有简写)
[[email protected] ~]# dhudywh 2>> date.txt
查证:
[[email protected] ~]# cat date.txt
bash: datt: 未找到命令…
bash: dhudywh: 未找到命令…
[追加]
举例1:输出定向
[[email protected] ~]# date 1> date.txt
[[email protected] ~]# cat date.txt
2020年 11月 24日 星期二 19:26:19 CST
[[email protected] ~]# date >> date.txt
[[email protected] ~]# cat date.txt
2020年 11月 24日 星期二 19:26:19 CST
2020年 11月 24日 星期二 19:26:41 CST
[[email protected] ~]# mkdir -v dir33 >> date.txt
[[email protected] ~]# cat date.txt
2020年 11月 24日 星期二 19:26:19 CST
2020年 11月 24日 星期二 19:26:41 CST
mkdir: 已创建目录 “dir33”
【mkdir 不可用 因为没有输出 加上-v可用 -v:显示创建过程(程序本身需要输出)】
举例2:错误输出定向
当某条命令产生错误时,才会有错误输出,存入文件中
[[email protected] ~]# ls /123456 2> date.txt
查证:
[[email protected] ~]# cat date.txt
ls: 无法访问/123456: 没有那个文件或目录
[[email protected] ~]# ls /jjjjjuuuuuhhh 2>> date.txt
查证:
[[email protected] ~]# cat date.txt
ls: 无法访问/123456: 没有那个文件或目录
ls: 无法访问/jjjjjuuuuuhhh: 没有那个文件或目录
可各自分类收集:
[[email protected] ~]# ls /home/ /hhhhhhh 1> yes.txt 2> no.txt
查证:
[[email protected] ~]# cat yes.txt no.txt
/home/:
file2
file4
hang
li
lijinhang
lll
test.txt
user01
user02
user09
user100
user200
user300
ls: 无法访问/hhhhhhh: 没有那个文件或目录
可用&(and)都收入一个文件:
[[email protected] ~]# ls /home/ /hhhhhhh &> date.txt
[[email protected] ~]# cat date.txt
ls: 无法访问/hhhhhhh: 没有那个文件或目录
/home/:
file2
file4
hang
li
lijinhang
lll
test.txt
user01
user02
user09
user100
user200
可收入垃圾桶中:
[[email protected] ~]# ls /home/ /hhhhhhh &> /dev/null
输入定向及结合案例
标准输入:<等价于0<
命令 < 文件 将指定文件作为命令的输入设备
[[email protected] ~]# 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
[[email protected] ~]# 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 :邮件接收人
. :结束符号
[[email protected] ~]# mail -s “abcdefg” user01
asdf
asd
zc
fg
.
EOT
查看邮件:
[[email protected] ~]# su - user01
上一次登录:二 11月 24 14:08:10 CST 2020pts/0 上
[[email protected] ~]$ 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
[[email protected] ~]# mail -s “12345” user01 < date.txt
原理:利用输入重定向,把文件内容代替人为的输入。
查证:略(同上)
管道
进程管道:
管道命令可以将多条命令组合起来,一次性完成复杂的处理任务
语法:command1 | command2 | command3 |…
指令1的标准输出作为指令2 的标准输入
举例:
[[email protected] ~]# cat /etc/passwd | tail -2
user300❌1008:1009::/home/user300:/bin/bash
user02❌1009:1011::/home/user02:/bin/bash
[[email protected] ~]# 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管道:
三通管道,即交给另一个程序处理,又保存一份副本
[[email protected] ~]# cat /etc/passwd |tee 78.txt |tail -1
user02❌1009:1011::/home/user02:/bin/bash
[将/etc/passwd的内容保存在78.txt中]
[[email protected] ~]# 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、环境准备,准备一些文件
[[email protected] ~]# touch /home/file{1…5}
[[email protected] ~]# cd /home
[[email protected] home]# ls
file1 file3 file5 li lll user01 user09 user200
file2 file4 hang lijinhang test.txt user02 user100 user300
2、需要删除部分文件
[[email protected] home]# vim files.txt
输入:
/home/file2
/home/file3
/home/file4
/home/file5
3、使用管道|
[[email protected] home]# cat files.txt | rm -rf
[[email protected] home]# ls
file1 file3 file5 hang lijinhang test.txt user02 user100 user300
file2 file4 files.txt li lll user01 user09 user200
[失败]
4、加上xargs
[[email protected] home]# cat files.txt |xargs rm -rf
[[email protected] home]# ls
file1 hang lijinhang test.txt user02 user100 user300
files.txt li lll user01 user09 user200
[删除成功]
但files.txt中文件还存在(不影响)
[[email protected] home]# cat files.txt |xargs rm -rvf
已删除"/home/file1"
(v 可显示删除过程)
[通过|xargs成功连接rm命令]
命令<文件1>文件2
将文件1作为命令的输入设备,该命令的执行结果输出到文件2中
[[email protected] ~]# cat < /etc/passwd > date.txt
查证:
[[email protected] ~]# 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 链接文件:
[[email protected] ~]# touch 1.txt
[[email protected] ~]# ln -s 1.txt 1.ttt
alias :别名
[[email protected] ~]# alias ll
alias ll=‘ls -l --color=auto’