Shell管道和过滤器

一、Shell管道

Shell 还有一种功能,就是可以将两个或者多个命令(程序或者进程)连接到一起,把一个命令的输出作为下一个命令的输入,以这种方式连接的两个或者多个命令就形成了管道(pipe)。

重定向和管道的区别:

  • 重定向操作符>将命令与文件连接起来,用文件来接收命令的输出;
  • 管道符|将命令与命令连接起来,用第二个命令来接收第一个命令的输出。

命令格式如下:

shell 复制代码
command > file
command1 | command1

1、Shell管道

Linux 管道使用竖线|连接多个命令,这被称为管道符。Linux 管道的具体语法格式如下:

shell 复制代码
command1 | command2
command1 | command2 [ | commandN... ]

当在两个命令之间设置管道时,管道符|左边命令的输出就变成了右边命令的输入。只要第一个命令向标准输出写入,而第二个命令是从标准输入读取,那么这两个命令就可以形成一个管道。

大部分的 Linux 命令都可以用来形成管道。

注意:

  • 管道里面的 command只能处理其正确的输出结果。
  • 管道符|与两侧的命令之间也可以不存在空格,但是推荐在管道符|和两侧的命令之间使用空格,以增加代码的可读性。

示例1:将 ls 命令的输出发送到 grep 命令

shell 复制代码
[root@centos7 tmp]#  ls -al | grep test.sh
-rw-r--r--   1 root root  168 3月   3 12:27 test.sh

示例2:将上述管道命令的输出结果发送到文件 output.txt 中

shell 复制代码
[root@centos7 tmp]# ls -al | grep test.sh  >output.txt
[root@centos7 tmp]# cat output.txt
-rw-r--r--   1 root root  168 3月   3 12:27 test.sh

示例3:查看指定程序的进程运行状态,并将输出重定向到文件中。

shell 复制代码
[root@centos7 tmp]# ps aux | grep httpd > /tmp/ps.output

2、管道与输入重定向

输入重定向操作符<可以在管道中使用,以用来从文件中获取输入,其基本语法:

shell 复制代码
command1 < input.txt | command2
command1 < input.txt | command2 -option | command3

示例:使用 tr 命令从 demo.txt 文件中获取输入,然后通过管道将输出发送给 sort 或 uniq 等命令:

shell 复制代码
[root@centos7 tmp]# cat demo.txt
百度一下
http://www.baidu.com/
shell 重定向
[root@centos7 tmp]# tr a-z A-Z <demo.txt | sort
HTTP://WWW.BAIDU.COM/
SHELL 重定向
百度一下
[root@centos7 tmp]# tr a-z A-Z <demo.txt | sort | uniq
HTTP://WWW.BAIDU.COM/
SHELL 重定向
百度一下
[root@centos7 tmp]#

3、管道与输出重定向

可以使用重定向操作符>或>>将管道中的最后一个命令的标准输出进行重定向,其语法如下所示:

shell 复制代码
command1 | command2 | ... | commandN > output.txt
command1 < input.txt | command2 | ... | commandN > output.txt

示例:使用 tr 命令将 demo.txt 文件中的内容转化为大写,并使用 sort 命令将内容排序,使用 uniq 命令去除重复的行,最后将输出重定向到文件 demo_new.txt。

shell 复制代码
[root@centos7 tmp]# tr a-z A-Z <demo.txt | sort | uniq >demo_new.txt
[root@centos7 tmp]# cat demo_new.txt
HTTP://WWW.BAIDU.COM/
SHELL 重定向
百度一下
[root@centos7 tmp]#

二、Shell过滤器

1、Shell过滤器

将几个命令通过管道符组合在一起就形成一个管道。通常,通过这种方式使用的命令就被称为过滤器。过滤器会获取输入,通过某种方式修改其内容,然后将其输出。

过滤器可以概括为以下两点:

  • 如果一个 Linux 命令是从标准输入接收它的输入数据,并在标准输出上产生它的输出数据(结果),那么这个命令就被称为过滤器。
  • 过滤器通常与 Linux 管道一起使用。

常用的被作为过滤器使用的命令如下所示:

下面通过实例来演示几个过滤器命令的使用。

2、在管道中使用grep命令

grep命令(global search regular expression and print out the line的缩写):用于全面搜索的正则表达式,并将结果输出。

人们通常会将grep命令与正则表达式搭配使用,参数作为搜索过程中的补充或对输出结果的筛选,命令模式十分灵‍活。

语法格式:grep 参数 文件名

常用参数:

示例1:查看 demo文件中的 www信息。

shell 复制代码
[root@centos7 tmp]# grep -i "Www" /tmp/demo.txt
http://www.baidu.com/
[root@centos7 tmp]# grep -i "Www" /tmp/demo.txt | less

示例2:查看系统中 HTTP 服务的进程信息。

shell 复制代码
[root@centos7 tmp]# ps auxwww | grep httpd

示例3:查找我们的程序列表中所有命令名中包含关键字 zip 的命令。

shell 复制代码
[root@centos7 tmp]# ls /bin /usr/bin | sort | uniq | grep zip
bunzip2
bzip2
bzip2recover
funzip
gpg-zip
gunzip
gzip
unzip
unzipsfx
zip
zipcloak
zipgrep
zipinfo
zipnote
zipsplit
[root@centos7 tmp]#

示例4:查看系统安装的 kernel 版本及相关的 kernel 软件包。

shell 复制代码
[root@centos7 tmp]# rpm -qa | grep kernel
kernel-headers-3.10.0-957.el7.x86_64
kernel-3.10.0-957.el7.x86_64
kernel-tools-3.10.0-957.el7.x86_64
kernel-tools-libs-3.10.0-957.el7.x86_64
abrt-addon-kerneloops-2.1.11-52.el7.centos.x86_64
[root@centos7 tmp]# 

3、在管道中使用 head 命令

有时,你不需要一个命令的全部输出,可能只需要命令的前几行输出。这时,就可以使用 head 命令.

head命令的功能是显示文件开头的内容,默认为前10行。

语法格式:head 参数 文件名

常用参数:

示例1:显示 ls 命令的前 10 行输出。

shell 复制代码
[root@centos7 tmp]# ls /usr/bin | head
[
a2p
ab
abrt-action-analyze-backtrace
abrt-action-analyze-c
abrt-action-analyze-ccpp-local
abrt-action-analyze-core
abrt-action-analyze-oops
abrt-action-analyze-python
abrt-action-analyze-vmcore
[root@centos7 tmp]#

示例2:显示 ls 命令的前 5 行内容。

shell 复制代码
[root@centos7 tmp]# ls /tmp | head -n 5
demo.txt
err.log
error.log
info.log
output.txt
[root@centos7 tmp]#

4、在管道中使用 wc 命令

wc 命令:wc命令(word count的缩写):其功能是统计文件的字节数、单词数、行数等信息,并将统计结果输出到终端界面。

基本语法格式:wc 参数 文件名

常见参数如下:

示例1:统计当前登录到系统的用户数。

shell 复制代码
[root@centos7 tmp]# who | wc -l
1

示例2:统计当前的 Linux 系统中的进程数。

shell 复制代码
[root@centos7 tmp]# ps -ef | wc -l
121

示例3:显示 ls 命令的前 5 行内容。然后统计当前内容的行数。

shell 复制代码
[root@centos7 tmp]# ls /tmp | head -n 5 | wc -l
5

-- 求知若饥,虚心若愚。