• Use redirection characters to control output to files. 使用重定向字符控制輸出到文件。
• Use piping to control output to other programs.使用管道控制輸出到其餘程序
進程管道
用法:command1 | command2 |command3 |...
[root@tianyun ~]# ll /dev/ |less
[root@tianyun ~]# ps aux |grep 'sshd'
[root@tianyun ~]# rpm -qa |grep 'httpd' //查詢全部安裝的軟件包,過濾包含 httpd 的包
[root@tianyun ~]# yum list |grep 'httpd' //
案例 1:將/etc/passwd 中的用戶按 UID 大小排序
[root@tianyun ~]# sort -t":" -k3 -n /etc/passwd //以: 分隔,將第三列按字數升序
[root@tianyun ~]# sort -t":" -k3 -n /etc/passwd -r //逆序
[root@tianyun ~]# sort -t":" -k3 -n /etc/passwd |head 顯示前10個
-t 指定字段分隔符--field-separator
-k 指定列
-n 按數值
案例 2:統計出最佔 CPU 的 5 個進程
[root@tianyun ~]# ps aux --sort=-%cpu |head -6
案例 3:統計當前/etc/passwd 中用戶使用的 shell 類型
思路:取出第七列(shell) | 排序(把相同歸類)| 去重
[root@tianyun ~]# awk -F: '{print $7}' /etc/passwd
[root@tianyun ~]# awk -F: '{print $7}' /etc/passwd |sort
[root@tianyun ~]# awk -F: '{print $7}' /etc/passwd |sort |uniq
[root@tianyun ~]# 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: 打印當前全部 IP
[root@dong ~]# ip a |grep 'inet ' 只顯示IPV4地址
inet 127.0.0.1/8 scope host lo
inet 192.168.1.2/24 brd 192.168.1.255 scope global eth0
[root@tianyun ~]# ip addr |grep 'inet ' |awk '{print $2}' |awk -F"/" '{print $1}'
127.0.0.1
192.168.2.115
案例 5:打印根分區已用空間的百分比(僅打印數字)
[root@tianyun ~]# df -P |grep '/$' |awk '{print $5}' |awk -F"%" '{print $1}'shell