管道
管道符 :"|"
-
作用:将前一个进程的输出(stdout)作为后一个进程的输入(stdin)。
-
输入输出
-
0 : 标准输入,程序或命令需要外部的某些程序传递相应的参数,才能正常运行
-
1 :标准输出,正确的执行结果
-
2 :标准错误,错误的执行结果
-
-
用法:
command1 | command2 | command3
与命令结合实现特殊功能
grep过滤
- 典例
1.从/etc/passwd文件中过滤zhangan的信息
[root@server1 ~]# cat /etc/passwd | grep zhangan
zhangan:x:1000:1000::/home/zhangan:/bin/bash
2.从系统中的已安装文件中筛选mariadb软件信息
[root@server1 ~]# rpm -qa | grep mariadb
mariadb-5.5.68-1.el7.x86_64
mariadb-libs-5.5.68-1.el7.x86_64
mariadb-server-5.5.68-1.el7.x86_64
3.从进程中过滤maridb相关的进程信息
[root@server1 ~]# ps -ef | grep mariadb
mysql 11336 11171 5 15:27 ? 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root 11374 1388 0 15:27 pts/0 00:00:00 grep --color=auto mariadb
wc统计
wc=word count
常用选项:
-l : line,总行数
-w : word,总单词数
-c : 总字符数
统计/etc/passwd共有多少行
[root@server1 ~]# cat /etc/passwd | wc -l
20
tr转换
tr=translate
- 典例
查看/etc/passwd文件中的前3行,要求小写字母转换为大写
[root@server1 ~]# head -3 /etc/passwd | tr 'a-z' 'A-Z'
ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
BIN:X:1:1:BIN:/BIN:/SBIN/NOLOGIN
DAEMON:X:2:2:DAEMON:/SBIN:/SBIN/NOLOGIN
cut分割
常用选项:
-d:指定分隔符
-f:指定字段
以:为分割,获取/etc/passwd中前三行的第一个字段(用户名)
[root@server1 ~]# head -3 /etc/passwd | cut -d: -f1
root
bin
daemon
xargs扩展支持管道符
-
作用:很多命令不支持 | 管道符来传递参数,但有时我们需要其支持,可以用xargs 命令扩展支持管道符
-
典例:搜索/etc目录下的所有".conf"结尾的文件信息,然后以详细列表形式显示
[root@server1 ~]# find /etc -name "*.conf" | ls -l
总用量 4
-rw-------. 1 root root 1243 7月 24 17:35 anaconda-ks.cfg
结果与要求不符,因为ls -l不支持管道符
[root@server1 ~]# find /etc -name "*.conf" | xargs ls -l
-rw-r--r--. 1 root root 55 8月 8 2019 /etc/asound.conf
-rw-r-----. 1 root root 246 8月 8 2019 /etc/audisp/audispd.conf
-rw-r-----. 1 root root 358 8月 8 2019 /etc/audisp/plugins.d/af_unix.conf
-rw-r-----. 1 root root 517 8月 8 2019 /etc/audisp/plugins.d/syslog.conf
-rw-r-----. 1 root root 805 8月 8 2019 /etc/audit/auditd.conf
-rw-r--r--. 1 root root 838 9月 30 2020 /etc/dbus-1/session.conf