标准输入输出

重定向 - 图1
标准输入(stdin)
标准输出(stdout)
标准错误输出(stderr)

在linux中,每个进程会有对应的进程号,假如:tail -2 /etc/passwd命令对应的进程号为1788,则会存在目录/proc/1788,/proc/1788/fd目录存在0,1,2……等文件,其中/etc/passwd是一个输入文件,使用一个编号表示。

上图中,键盘、显示器在linux中都表示为一个设备文件,每个设备文件都用一个编号表示。

  1. #查看当前进程
  2. [root@kedacom fd]# ps
  3. PID TTY TIME CMD
  4. 206459 pts/1 00:00:00 su
  5. 206465 pts/1 00:00:00 bash
  6. 221644 pts/1 00:00:00 ps
  7. #执行命令后,ctrl+z在后台运行
  8. [root@kedacom fd]# tail -f /etc/passwd
  9. tcpdump:x:72:72::/:/sbin/nologin
  10. tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
  11. uuidd:x:996:995:UUID generator helper daemon:/var/lib/libuuid:/sbin/nologin
  12. ntp:x:38:38::/etc/ntp:/sbin/nologin
  13. tomcat:x:995:994::/home/tomcat:/bin/false
  14. nginx:x:994:993:nginx user:/var/cache/nginx:/sbin/nologin
  15. es:x:1002:1002::/home/es:/bin/bash
  16. mysql:x:993:1003::/home/mysql:/bin/bash
  17. kedacom:x:1004:0::/home/kedacom:/bin/bash
  18. devupdate:x:1005:50::/var/nms/devversion:/bin/bash
  19. ^Z
  20. [1]+ 已停止 tail -f /etc/passwd
  21. #再次查看当前进程,存在一个tail的进程,进程号为221735
  22. [root@kedacom fd]# ps
  23. PID TTY TIME CMD
  24. 206459 pts/1 00:00:00 su
  25. 206465 pts/1 00:00:00 bash
  26. 221735 pts/1 00:00:00 tail
  27. 221756 pts/1 00:00:00 ps
  28. #查看tail进程相关的文件
  29. [root@kedacom fd]# ls -l /proc/221735/fd
  30. 总用量 0
  31. lrwx------ 1 root root 64 1 31 17:32 0 -> /dev/pts/1
  32. lrwx------ 1 root root 64 1 31 17:32 1 -> /dev/pts/1
  33. lrwx------ 1 root root 64 1 31 17:31 2 -> /dev/pts/1
  34. lr-x------ 1 root root 64 1 31 17:32 3 -> /etc/passwd
  35. lr-x------ 1 root root 64 1 31 17:32 4 -> anon_inode:inotify

进程相关文件说明:
0 - 标准输入,键盘
1 - 标准输出,显示器
2 - 标准错误输出,显示器
3 - tail命令操作的文件/etc/passwd
其中0/1/2/3是文件描述符

  1. #tail -f 带两个参数
  2. [root@kedacom fd]# tail -f /etc/passwd /etc/shadow
  3. ==> /etc/passwd <==
  4. tcpdump:x:72:72::/:/sbin/nologin
  5. tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
  6. uuidd:x:996:995:UUID generator helper daemon:/var/lib/libuuid:/sbin/nologin
  7. ntp:x:38:38::/etc/ntp:/sbin/nologin
  8. tomcat:x:995:994::/home/tomcat:/bin/false
  9. nginx:x:994:993:nginx user:/var/cache/nginx:/sbin/nologin
  10. es:x:1002:1002::/home/es:/bin/bash
  11. mysql:x:993:1003::/home/mysql:/bin/bash
  12. kedacom:x:1004:0::/home/kedacom:/bin/bash
  13. devupdate:x:1005:50::/var/nms/devversion:/bin/bash
  14. ==> /etc/shadow <==
  15. tcpdump:!!:18711::::::
  16. tss:!!:18711::::::
  17. uuidd:!!:18711::::::
  18. ntp:!!:18711::::::
  19. tomcat:!!:18711::::::
  20. nginx:!!:18711::::::
  21. es:!!:18711:0:99999:7:::
  22. mysql:!!:18717::::::
  23. kedacom:$6$0QI3gjLR$l9YAEizlY5WZ4mIW.minjbxiqDgFqjoJu425fVsYbvsK0YK/ils4yE6uXrS1N9MGhxgheJEmw7inp8Oj88Ms/1:18732:0:99999:7:::
  24. devupdate:$6$zHS/b2ra$7utpkEVqsSwU/3UR5EwPHalKo63QeTZYCGUvGHegeAa9IO5zMv1uaPOHs8UmeE62rh4.d4vqMU1/bdUQOM.wB.:18954:0:99999:7:::
  25. ^Z
  26. [2]+ 已停止 tail -f /etc/passwd /etc/shadow
  27. [root@kedacom fd]# ps
  28. PID TTY TIME CMD
  29. 206459 pts/1 00:00:00 su
  30. 206465 pts/1 00:00:00 bash
  31. 221735 pts/1 00:00:00 tail
  32. 222697 pts/1 00:00:00 tail
  33. 222714 pts/1 00:00:00 ps
  34. #此时多了1个文件/etc/shadow
  35. [root@kedacom fd]# ll /proc/222697/fd
  36. 总用量 0
  37. lrwx------ 1 root root 64 1 31 17:40 0 -> /dev/pts/1
  38. lrwx------ 1 root root 64 1 31 17:40 1 -> /dev/pts/1
  39. lrwx------ 1 root root 64 1 31 17:39 2 -> /dev/pts/1
  40. lr-x------ 1 root root 64 1 31 17:40 3 -> /etc/passwd
  41. lr-x------ 1 root root 64 1 31 17:40 4 -> /etc/shadow
  42. lr-x------ 1 root root 64 1 31 17:40 5 -> anon_inode:inotify

进程相关文件说明:
0 - 标准输入,键盘
1 - 标准输出,显示器
2 - 标准错误输出,显示器
3 - tail命令操作的文件/etc/passwd
4 - tail命令操作的文件/etc/shadow

linux中所有设备都在/dev目录下,一个设备就是一个文件,例如:光驱、网卡等设备。虚拟设备也在此目录下。

  1. [root@kedacom fd]# ll /dev/std*
  2. lrwxrwxrwx 1 root root 15 1 25 16:25 /dev/stderr -> /proc/self/fd/2
  3. lrwxrwxrwx 1 root root 15 1 25 16:25 /dev/stdin -> /proc/self/fd/0
  4. lrwxrwxrwx 1 root root 15 1 25 16:25 /dev/stdout -> /proc/self/fd/1

标准输入输出可通过ll /dev/std*查看,/dev/stderr /dev/stdin /dev/stdout都是逻辑设备,指向不同进程文件,其中self就是各进程的进程号。

重定向

定义:将标准输入、标注输出、标注错误输出通过别的方式替代,就是所谓的重定向

类型 操作符 用途
重定向标准输出 > 将命令的执行结果输出到指定的文件中,而不是直接显示在屏幕上
>> 将命令执行的结果追加输出到执行的文件中
重定向标准输入 < 将命令中接收输入的途径由默认的键盘更改为指定的文件
Here document << 指定输入结束符

标准输出重定向

ifconfig eth0内容默认是输出到屏幕中,可重定向到文件out中

[root@kedacom test]# ifconfig eth0 1>out
[root@kedacom test]# cat out
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.248.63  netmask 255.255.248.0  broadcast 172.16.255.255
        ether 00:14:10:29:9a:4b  txqueuelen 1000  (Ethernet)
        RX packets 15909392  bytes 2033875289 (1.8 GiB)
        RX errors 0  dropped 215542  overruns 0  frame 0
        TX packets 13396155  bytes 1955902801 (1.8 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device memory 0x9d400000-9d41ffff

上面命令中,1可以不填写,ifconfig eth0 >out实现相同的功能

[root@kedacom test]# ifconfig eth0 > out1
[root@kedacom test]# cat out1
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.248.63  netmask 255.255.248.0  broadcast 172.16.255.255
        ether 00:14:10:29:9a:4b  txqueuelen 1000  (Ethernet)
        RX packets 15915873  bytes 2034524614 (1.8 GiB)
        RX errors 0  dropped 215652  overruns 0  frame 0
        TX packets 13401214  bytes 1956469071 (1.8 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device memory 0x9d400000-9d41ffff

标准错误输出重定向

[root@kedacom test]# ifconfig eth9
eth9: error fetching interface information: Device not found

ifconfig eth9输出的是错误内容,可将错误输出重定向到error文件中。
注意:命令中的2必须填写,与标准输出重定向不同

[root@kedacom test]# ifconfig eth9 2>error
[root@kedacom test]# cat error
eth9: error fetching interface information: Device not found

标准输出和错误输出重定向

场景1:执行的命令输出存在正确输出、错误输出时,可分别将正确输出到一个文件,错误输出到另一个文件中
例如:普通用户执行命令find /etc -name “*.conf”后,会有部分文件没有权限,打印错误信息,可将正确输出到out文件,错误输出到error文件

[kedacom@kedacom ~]$ find /etc -name "*.conf" 1>out 2>error
[kedacom@kedacom ~]$ cat out
/etc/resolv.conf
/etc/logrotate.conf
/etc/asound.conf
/etc/openldap/ldap.conf
/etc/libuser.conf
/etc/sudo-ldap.conf
/etc/sudo.conf
/etc/host.conf
/etc/yum/pluginconf.d/nkucplugin.conf
......
[kedacom@kedacom ~]$ cat error 
find: ‘/etc/grub.d’: 权限不够
find: ‘/etc/selinux/targeted/active’: 权限不够
find: ‘/etc/selinux/final’: 权限不够
find: ‘/etc/pki/CA/private’: 权限不够
find: ‘/etc/pki/rsyslog’: 权限不够
find: ‘/etc/lvm/archive’: 权限不够
find: ‘/etc/lvm/backup’: 权限不够
find: ‘/etc/lvm/cache’: 权限不够
find: ‘/etc/polkit-1/rules.d’: 权限不够
find: ‘/etc/polkit-1/localauthority’: 权限不够
find: ‘/etc/ntp/crypto’: 权限不够
find: ‘/etc/nginx/client_body_temp’: 权限不够
find: ‘/etc/nginx/proxy_temp’: 权限不够
find: ‘/etc/nginx/fastcgi_temp’: 权限不够
find: ‘/etc/nginx/uwsgi_temp’: 权限不够
find: ‘/etc/nginx/scgi_temp’: 权限不够

场景2:将正确输出和错误输出都重定向到同一个文件
例如:find /etc -name “*.conf”将正确、错误输出都重定向到all文件

[kedacom@kedacom ~]$ find /etc -name "*.conf" &>all
[kedacom@kedacom ~]$ cat all
/etc/resolv.conf
find: ‘/etc/grub.d’: 权限不够
/etc/logrotate.conf
/etc/asound.conf
/etc/openldap/ldap.conf
/etc/libuser.conf
/etc/sudo-ldap.conf
/etc/sudo.conf
/etc/host.conf
/etc/yum/pluginconf.d/nkucplugin.conf
/etc/yum/pluginconf.d/langpacks.conf
/etc/yum/protected.d/systemd.conf
/etc/yum/version-groups.conf
/etc/selinux/semanage.conf
find: ‘/etc/selinux/targeted/active’: 权限不够
/etc/selinux/targeted/setrans.conf
find: ‘/etc/selinux/final’: 权限不够
......

区别>与>>

filename 如果filename不存在,则创建该文件;若存在,则覆盖文件中的内容
>>filename 将内容追加到filename文件中

#没有testfile文件则创建
[root@kedacom test]# echo "123456" > testfile
[root@kedacom test]# cat testfile 
123456
#覆盖testfile文件的内容
[root@kedacom test]# echo "222222" > testfile 
[root@kedacom test]# cat testfile 
222222
#追加内容到testfile文件
[root@kedacom test]# echo "abcdefg" >> testfile 
[root@kedacom test]# cat testfile 
222222
abcdefg

说明:>>命令可用于观察某个命令的执行结果,用于记录每次执行的情况。例如:内存、cpu情况等。

使用重定向合并文件

[root@kedacom test]# echo "123456" > a
[root@kedacom test]# echo "abcdef" > b
[root@kedacom test]# cat a b > ab
[root@kedacom test]# cat ab
123456
abcdef

重定向的本质

重定向是将对应的输出制定重定向文件

#输入命令,ctrl+z
[root@kedacom test]# tail -f /etc/passwd 1>test.txt
^Z
[1]+  已停止               tail -f /etc/passwd > test.txt
#查看tail -f的进程号
[root@kedacom test]# ps
   PID TTY          TIME CMD
 24110 pts/1    00:00:00 su
 24111 pts/1    00:00:00 bash
 24145 pts/1    00:00:00 su
 25787 pts/1    00:00:00 su
 25792 pts/1    00:00:00 bash
 27075 pts/1    00:00:00 tail
 27087 pts/1    00:00:00 ps
206459 pts/1    00:00:00 su
206465 pts/1    00:00:00 bash
#查看tail -f进程文件设备指向的文件
[root@kedacom test]# ll /proc/27075/fd
总用量 0
lrwx------ 1 root root 64 1月  31 22:13 0 -> /dev/pts/1
l-wx------ 1 root root 64 1月  31 22:13 1 -> /opt/test/test.txt
lrwx------ 1 root root 64 1月  31 22:13 2 -> /dev/pts/1
lr-x------ 1 root root 64 1月  31 22:13 3 -> /etc/passwd
lr-x------ 1 root root 64 1月  31 22:13 4 -> anon_inode:inotify

标准输入重定向