Loading

Linux-I/O重定向和管道

Linux I/O重定向

  • 标准输入(stdin):文件描述符0
  • 标准输入(stdout):文件描述符1
  • 标准错误(stderr):文件描述符2

file descriptors(FD,文件描述符 或 Process I/O channels);

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

[root@centos7-1 ~]# ls /proc/$$/fd
0  1  2  255

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

输出重定向(覆盖、追加)

  • 正确输出:1>  1>>  等价于 > >>
  • 错误输出:2>  2>>

输出重定向(覆盖)

[root@centos7-1 ~]# date 1> date.txt

输出重定向(追加)

[root@centos7-1 ~]# date >> date.txt

错误输出重定向

[root@centos7-1 ~]# ls /home/ /aaaa >list.txt
ls: 无法访问/aaaa: 没有那个文件或目录
[root@centos7-1 ~]# ls /home/ /aaaa >list.txt 2>err.txt      //重定向到不同的位置

正确和错误都输出到相同位置 &>

[root@centos7-1 ~]# ls /home/ /aaaa &>list.txt          //混合输出

正确和错误都输出到相同位置 2>&1

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

重定向到空设备/dev/null

[root@centos7-1 ~]# ls /home/ /aaaa >list.txt 2>/dev/null     //空设备,即将产生的输出丢掉

[root@centos7-1 ~]# ls /home/ /aaaa &>/dev/null     //空设备,即将产生的输出丢掉

/dev/null 补充

/dev/null:是一个空设备,黑洞,任何文件都可以扔进去,但是看不见

如果/dev/null设备被删除怎么办? rm -f /dev/null

1、手动创建

# mknod -m 666 /dev/null c 1 3 
[root@centos7-1 ~]# ll /dev/null /dev/zero 
crw-rw-rw- 1 root root  1,     3 11月  7 12:23 /dev/null
crw-rw-rw- 1 root root  1,     5 11月  7 12:23 /dev/zero
                    主设备号  从设备号
                      MAJOR   MINOR

主设备号相同:表示为同一种设备类型,也可以认为keme使用的是相同的驱动

从设备号:在同一类型中的一个序号

[root@centos7-1 ~]# ll /dev/null /dev/vda1 /etc/hosts
crw-rw-rw-  1 root root   1, 3 11月  7 12:23 /dev/null
brw-rw----  1 root disk 252, 1 11月  7 12:23 /dev/vda1
-rw-r--r--. 1 root root    172 10月 29 14:38 /etc/hosts
c表示字符设备

普通文件和设备文件的区别:

从表面上看,普通文件有大小;块设备文件没有大小,有主设备号和从设备号。

字符设备和快设备的区别:

字符设备没有缓存,块设备有缓存

脚本中使用重定向

案例1:脚本中使用重定向
# vim ping.sh 
#!/usr/bin/bash
ping -c1 172.16.120.254 &>/dev/null
if [ $? -eq 0 ];then
    echo "up.."
else
    echo "down.."
fi
# bash ping.sh


案例2:脚本中使用重定向
# vim ping2.sh 
#!/usr/bin/bash
ping -c1 172.16.120.254 &>/dev/null
if [ $? -eq 0 ];then
    echo "172.16.120.254 up.."  > /up.txt
else
    echo "172.16.120.254 down.." >/down.txt
fi
# bash ping2.sh
示例

输入重定向

标准输入: <     等价于   0<

案例1

[root@centos7-1 ~]# mail -s "ssss" alice               //没有改变输入的方向,默认键盘
111
222
333            
^D
[root@centos7-1 ~]# su - alice
[alice@centos7-1 ~]$ mail
Mail version 8.1 6/6/93.  Type ? for help.
"/var/spool/mail/alice": 1 message 1 new
>N  1 root@tianyun.local  Mon Oct 29 14:09  18/657   "ssss"
& 

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

案例2

[root@centos7-1 ~]# grep 'root'                         //没有改变输入的方向,默认键盘,此时等待输入...
yang sss
sssrootssss..
sssrootssss..

[root@centos7-1 ~]# grep 'root' < /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

案例3

[root@centos7-1 ~]# dd if=/dev/zero of=/file1.txt bs=1M count=2
[root@centos7-1 ~]# dd </dev/zero >/file2.txt bs=1M count=20

案例4 mysql表结构导入

[root@centos7-1 ~]# mysql -uroot -p123 < bbs.sql

重定向综合案例

综合案例1:利用重定向建立多行的文件

[root@centos7-1 ~]# echo "111" > file1.txt
[root@centos7-1 ~]# cat file1.txt 
111

[root@centos7-1 ~]# cat >file2.txt
111
222
333
444
^D
[root@centos7-1 ~]# cat file2.txt 

请问:file2.txt有几行?


[root@centos7-1 ~]# cat >>file3.txt
aaa
bbb
ccc
ddd
^D
[root@centos7-1 ~]# cat file3.txt 

请问:file3.txt有几行?

[root@centos7-1 ~]# cat >file4 <<EOF
> 111
> 222
> 333
> EOF
[root@centos7-1 ~]# cat file4
111
222
333
View Code

综合案例2: 脚本中利用重定向打印消息

[root@centos7-1 ~]# vim yang.sh
#!/usr/bin/bash
cat <<-EOF
+------------------------------------------------+
|                                                |
|               ======================    |
|                 虚拟机基本管理centos           |
|                                              |
|               ======================     |
|               1. 安装虚拟机                    |             
|               2. 重置所有Linux虚拟机           |
|               3. 重置Windows虚拟机             |
|               4. 重置Windows虚拟机  [完全]     |
|               5. 重置指定的虚拟机              |
|               q. 退出管理程序                  | 
|                                                |
+------------------------------------------------+
EOF
View Code

综合案例3

[root@centos7-1 ~]# ls; date &>/dev/null          //希望两条命令输出都重定向

[root@centos7-1 ~]# ls &>/dev/null; date &>/dev/null

[root@centos7-1 ~]# (ls; date) &>/dev/null

[root@centos7-1 ~]# (while :; do date; sleep 2; done) &    //在后台运行,但输出依然在终端显示

[root@centos7-1 ~]# (while :; do date; sleep 2; done) &>date.txt &
[1] 6595
[root@centos7-1 ~]# tailf date.txt 
Tue Apr 12 22:04:32 CST 2017
Tue Apr 12 22:04:34 CST 2017
Tue Apr 12 22:04:36 CST 2017
Tue Apr 12 22:04:38 CST 2017
Tue Apr 12 22:04:40 CST 2017
Tue Apr 12 22:04:42 CST 2017
Tue Apr 12 22:04:44 CST 2017
Tue Apr 12 22:04:46 CST 2017
Tue Apr 12 22:04:48 CST 2017

[root@centos7-1 ~]# jobs
[1]+  Running                 ( while :; do
    date; sleep 2;
done ) &>date.txt &
[root@centos7-1 ~]# kill %1
[root@centos7-1 ~]# jobs
View Code

进程管道Piping

进程管道

用法:command1 | command2 | command3 |....

[root@centos7-1 ~]# ll /dev/ |less
[root@centos7-1 ~]# ps aux |grep 'sshd'
[root@centos7-1 ~]# rpm -qa |grep 'httpd'    //查询所有安装的软件包,过滤包含httpd的包
[root@centos7-1 ~]# yum list |grep 'httpd'
  • 案例1:将/etc/password中的用户按UID大小排序
    [root@centos7-1 ~]# sort -t":" -k3 -n /etc/passwd   //以: 分隔,将第三列按字数升序
    [root@centos7-1 ~]# sort -t":" -k3 -n /etc/passwd -r    //逆序
    [root@centos7-1 ~]# sort -t":" -k3 -n /etc/passwd |head
    
    -t 指定字段分隔符--field-separator
    -k 指定列
    -n 按数值
  • 案例2:统计出最占CPU的5个进程
    [root@centos7-1 ~]# ps aux --sort=-%cpu |head -6
  • 案例3:统计当前/etc/password中用户使用的shell类型
    [root@centos7-1 ~]# awk -F: '{print $7}' /etc/passwd 
    [root@centos7-1 ~]# awk -F: '{print $7}' /etc/passwd |sort
    [root@centos7-1 ~]# awk -F: '{print $7}' /etc/passwd |sort |uniq
    [root@centos7-1 ~]# awk -F: '{print $7}' /etc/passwd |sort |uniq -c
          2 /bin/bash
          1 /bin/sync
          1 /sbin/halt
         41 /sbin/nologin
          1 /sbin/shutdown
  • 案例4:统计网站的访问情况 top5
    [root@centos7-1 ~]# 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@centos7-1 ~]# ss -an |grep :80 |awk -F":" '{print $8}' |sort |uniq -c |sort -k1 -rn |head -n 20 
  • 案例5:打印当前所有IP
    [root@centos7-1 ~]# ip addr |grep 'inet ' |awk '{print $2}' |awk -F"/" '{print $1}' 
    192.168.122.205
  • 案例6:打印根分区已用空间的百分比(仅打印数字)
    [root@centos7-1 ~]# df -P  |grep '/$' |awk '{print $5}' |awk -F"%" '{print $1}'

tee管道

[root@centos7-1 ~]#  ip addr |grep 'inet ' |tee ip.txt |awk -F"/" '{print $1}' |awk '{print $2}'
127.0.0.1
192.168.122.205
[root@centos7-1 ~]# cat ip.txt 
    inet 127.0.0.1/8 scope host lo
    inet 192.168.122.205/24 brd 192.168.122.255 scope global eth0
[root@centos7-1 ~]# ip addr |grep 'inet ' |tee -a ip.txt |awk -F"/" '{print $1}' |awk '{print $2}'
127.0.0.1
192.168.122.205
[root@centos7-1 ~]# date |tee date.txt
2018年 11月 07日 星期三 14:50:04 CST
[root@centos7-1 ~]# cat date.txt 
2018年 11月 07日 星期三 14:50:04 CST

参数传递Xargs

awk sed grep sort uniq less more xargs
xargs: ls cp rm

  • 案例1
    [root@localhost ~]# touch /home/file{1..5}
    
    [root@localhost ~]# vim files.txt 
    /home/file1
    /home/file2
    /home/file3
    /home/file4
    /home/file5
    
    [root@localhost ~]# cat files.txt |ls -l
    [root@localhost ~]# cat files.txt |rm -rvf
    
    cont.
    [root@localhost ~]# cat files.txt |xargs ls -l           
    -rw-r--r--. 1 root root 0 Mar 11 10:35 /home/file1
    -rw-r--r--. 1 root root 0 Mar 11 10:35 /home/file2
    -rw-r--r--. 1 root root 0 Mar 11 10:35 /home/file4
    -rw-r--r--. 1 root root 0 Mar 11 10:35 /home/file5
    
    [root@localhost ~]# cat files.txt |xargs rm -rvf          
    removed ‘/home/file1’
    removed ‘/home/file2’
    removed ‘/home/file4’
    removed ‘/home/file5’
  • 案例2
    [root@localhost ~]# touch /home/file{1..5}
    [root@localhost ~]# cat files.txt |xargs -I {} ls -l {}
    -rw-r--r--. 1 root root 0 Mar 11 10:40 /home/file1
    -rw-r--r--. 1 root root 0 Mar 11 10:40 /home/file2
    -rw-r--r--. 1 root root 0 Mar 11 10:40 /home/file4
    -rw-r--r--. 1 root root 0 Mar 11 10:40 /home/file5
    
    [root@localhost ~]# cat files.txt |xargs -I {} cp -rvf {} /tmp
    ‘/home/file1’ -> ‘/tmp/file1’
    ‘/home/file2’ -> ‘/tmp/file2’
    ‘/home/file4’ -> ‘/tmp/file4’
    ‘/home/file5’ -> ‘/tmp/file5’
    
    [root@localhost ~]# cat files.txt |xargs -I YANG cp -rvf YANG /var/tmp
    ‘/home/file1’ -> ‘/var/tmp/file1’
    ‘/home/file2’ -> ‘/var/tmp/file2’
    ‘/home/file4’ -> ‘/var/tmp/file4’
    ‘/home/file5’ -> ‘/var/tmp/file5’
  • 案例3
    [root@localhost ~]# find /etc -iname "*ifcfg*" |xargs -I {} cp -rf {} /tmp

     

 

posted @ 2018-11-07 15:01  别来无恙-  阅读(1450)  评论(0编辑  收藏  举报