千锋教育-做有情怀、有良心、有品质的职业教育机构

400-811-9990
手机站
千锋教育

千锋学习站 | 随时随地免费学

千锋教育

扫一扫进入千锋手机站

领取全套视频
千锋教育

关注千锋学习站小程序
随时随地免费学习课程

上海
  • 北京
  • 郑州
  • 武汉
  • 成都
  • 西安
  • 沈阳
  • 广州
  • 南京
  • 深圳
  • 大连
  • 青岛
  • 杭州
  • 重庆
当前位置:杭州千锋IT培训  >  技术干货  >  重定向和文件描述符

重定向和文件描述符

来源:千锋教育
发布人:qyf
时间: 2023-01-16 16:52:28

  标准输入

  标准输出

  标准正确输出

  标准错误输出

image-20200216140619668

  File Descriptors 简称fd 或 Process I/O channels

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

  [root@qfedu.com ~]# ls /proc/$$/fd

  0 1 2 3 4

  0, 1, and 2, known as standard input, standard output, and standard error

  使用lsof查看某个进程所使用的所有文件

  [root@wing fd]# lsof -p 3626

  输出重定向

  正确输出: 1> 1>> 等价于 > >>

  错误输出: 2> 2>>

  案例1:输出重定向(覆盖)

  [root@qfedu.com ~]# date 1> date.txt

image-20200216141216571

  案例2:输出重定向(追加)

  [root@qfedu.com ~]# date >> date.txt

image-20200216141201904

  案例3:错误输出重定向

  [root@qfedu.com ~]# ls /home/ /aaaaaaaaa >list.txt

  ls: 无法访问/aaaaaaaaa: 没有那个文件或目录

  [root@qfedu.com ~]# ls /home/ /aaaaaaaaa >list.txt 2>error.txt //重定向到不同的位置

image-20200216141144728

  案例4: 正确和错误都输入到相同位置

  [root@qfedu.com ~]# ls /home/ /aaaaaaaaa &>list.txt //混合输出

image-20200216141135061

  案例5: 正确和错误都输入到相同位置

  [root@qfedu.com ~]# ls /home/ /aaaaaaaaa >list.txt 2>&1 //重定向到相同的位置

image-20200216141124151

  案例6:重定向到空设备/dev/null

  [root@qfedu.com ~]# ls /home/ /aaaaaaaaa >list.txt 2>/dev/null //空设备,即将产生的输出丢掉[root@qfedu.com ~]# ls /home/ /aaaaaaaaa &>/dev/null //空设备,即将产生的输出丢掉

image-20200216141111550

  输入重定向

  标准输入:

  < 等价 0<

  案例1:

  [root@qfedu.com ~]# mail alice //没有改变输入的方向,默认键盘

  Subject: hello

  1111

  2222

  3333

  .

  EOT

  [root@qfedu.com ~]# su - alice

  [alice@wing ~]$ mail

  Heirloom Mail version 12.5 7/5/10. Type ? for help.

  "/var/spool/mail/alice": 1 message 1 new

  >N 1 root Mon Jul 31 15:16 20/617 "hello"

  [root@qfedu.com ~]# mail -s "test01" alice << /etc/hosts //输入重定向,来自于文件

  案例2:

  [root@qfedu.com ~]# grep 'root' //没有改变输入的方向,默认键盘,此时等待输入...

  yang sss

  sssrootssss..

  sssrootssss..

  [root@qfedu.com ~]# grep 'root' < /etc/passwd

  root:x:0:0:root:/root:/bin/bash

  operator:x:11:0:operator:/root:/sbin/nologin

  案例3:

  [root@qfedu.com ~]# dd if=/dev/zero of=/file1.txt bs=1M count=2

  [root@qfedu.com ~]# dd /file2.txt bs=1M count=20

  案例4:mysql表结构导入

  [root@qfedu.com ~]# mysql -uroot -p123 < bbs.sql

  案例5:at

  [root@qfedu.com ~]# at now +5 min

  at> useradd yang99

  at>

  job 1 at Mon Jul 31 15:29:00 2017

  [root@qfedu.com ~]# vim at.txt

  sudo useradd yang100

  sudo useradd yang102

  [root@qfedu.com ~]# at now +2 min <a.txt< p="">

  job 2 at Mon Jul 31 15:27:00 2017

  重定向命令

  tee命令

  Linux tee命令用于读取标准输入的数据,并将其内容输出成文件。

  tee指令会从标准输入设备读取数据,将其内容输出到标准输出设备,同时保存成文件。

  语法

  [root@qfedu.com ~]# tee [-ai][--help][--version][文件...]

  参数:

  -a或--append  附加到既有文件的后面,而非覆盖它.

  -i或--ignore-interrupts  忽略中断信号。

  --help  在线帮助。

  --version  显示版本信息。

  实例

  使用指令"tee"将用户输入的数据同时保存到文件"file1"和"file2"中,输入如下命令:

  [root@qfedu.com ~]# tee file1 file2 #在两个文件中复制内容

  以上命令执行后,将提示用户输入需要保存到文件的数据,如下所示:

  My Linux #提示用户输入数据 My Linux #输出数据,进行输出反馈

  此时,可以分别打开文件"file1"和"file2",查看其内容是否均是"My Linux"即可判断指令"tee"是否执行成功。

  管道

image-20200216141934730

  匿名管道

  [root@qfedu.com ~]# ll /dev/ | less

  [root@qfedu.com ~]# ps aux | grep 'sshd'

  [root@qfedu.com ~]# rpm -qa | grep 'httpd' //查询所有安装的软件包,过滤包含httpd的包

  [root@qfedu.com ~]# yum list | grep 'httpd'

  命名管道

  创建命名管道文件:

  # mkfifo /tmp/tmpfifo

  # file /tmp/tmpfifo

  /tmp/tmpfifo: fifo (named pipe)

  把指令结果放入管道先:

  # rpm -qa > /tmp/tmpfifo

  新建一个终端从命名管道内拿内容:

  # grep bash /tmp/tmpfifo

  bash-4.1.2-14.el6.x86_64

  实例

  案例1:将/etc/passwd中的用户按UID大小排序

  [root@qfedu.com ~]# sort -t":" -k3 -n /etc/passwd //以: 分隔,将第三列按字数升序

  [root@qfedu.com ~]# sort -t":" -k3 -n /etc/passwd -r //逆序

  [root@qfedu.com ~]# sort -t":" -k3 -n /etc/passwd |head

  -t 指定字段分隔符--field-separator

  -k 指定列

  -n 按数值

  案例2:统计出最占CPU的5个进程

  [root@qfedu.com ~]# ps aux --sort=-%cpu |head -6

  案例3:统计当前/etc/passwd中用户使用的shell类型

  思路:取出第七列(shell) | 排序(把相同归类)| 去重

  [root@qfedu.com ~]# awk -F: '{print $7}' /etc/passwd

  [root@qfedu.com ~]# awk -F: '{print $7}' /etc/passwd |sort

  [root@qfedu.com ~]# awk -F: '{print $7}' /etc/passwd |sort |uniq

  [root@qfedu.com ~]# awk -F: '{print $7}' /etc/passwd |sort |uniq -c

  131 /bin/bash

  1 /bin/sync

  1 /sbin/halt

  63 /sbin/nologin

  1 /sbin/shutdown

  -F: 指定字段分隔符

  $7 第七个字段

  案例4: 统计网站的访问情况 top 20

  思路:打印所有访问的连接 | 过滤访问网站的连接 | 打印用户的IP | 排序 | 去重

  [root@qfedu.com ~]# yum -y install httpd

  [root@qfedu.com ~]# systemctl start httpd

  [root@qfedu.com ~]# systemctl stop firewalld

  [root@qfedu.com ~]# ss -an |grep :80 |awk -F":" '{print $8}' |sort |uniq -c

  4334 192.168.0.66

  1338 192.168.10.11

  1482 192.168.10.125

  44 192.168.10.183

  3035 192.168.10.213

  375 192.168.10.35

  362 192.168.10.39

  [root@qfedu.com ~]# ss -an |grep :80 |awk -F":" '{print $8}' |sort |uniq -c |sort -k1 -rn |head -n 20

  案例5: 打印当前所有IP

  [root@qfedu.com ~]# ip addr |grep 'inet ' |awk '{print $2}' |awk -F"/" '{print $1}'

  127.0.0.1

  192.168.2.115

  案例6:打印根分区已用空间的百分比(仅打印数字)

  [root@qfedu.com ~]# df -P |grep '/$' |awk '{print $5}' |awk -F"%" '{print $1}'

声明:本站稿件版权均属千锋教育所有,未经许可不得擅自转载。

猜你喜欢LIKE

vue3.0和2.0的区别

2023-04-20

接口测试属于功能测试吗

2023-04-12

软件测试流程分几个阶段?

2023-04-11

最新文章NEW

学习c语言用什么软件

2023-04-14

hadoop需要什么基础

2023-04-10

java框架是什么意思

2023-03-20

相关推荐HOT

更多>>

快速通道 更多>>

最新开班信息 更多>>

网友热搜 更多>>