管道命令操作符是:”|”,它仅能处理经由前面一个指令传出的正确输出信息,也就是 standard output 的信息,对于 stdandard
error 信息没有直接处理能力。然后,传递给下一个命令,作为标准的输入 standard input.

  • 管道命令使用说明:

先看下下面图:

command1正确输出,作为command2的输入 然后comand2的输出作为,comand3的输入 ,comand3输出就会直接显示在屏幕上面了。

通过管道之后:comand1,comand2的正确输出不显示在屏幕上面

注意:

1、管道命令只处理前一个命令正确输出,不处理错误输出

2、管道命令右边命令,必须能够接收标准输入流命令才行。

实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[chengmo@centos5 shell]$ cat test.sh | grep -n 'echo'
5:    echo "very good!";
7:    echo "good!";
9:    echo "pass!";
11:    echo "no pass!";
#读出test.sh文件内容,通过管道转发给grep 作为输入内容
 
[chengmo@centos5 shell]$ cat test.sh test1.sh | grep -n 'echo'
cat: test1.sh: 没有那个文件或目录
5:    echo "very good!";
7:    echo "good!";
9:    echo "pass!";
11:    echo "no pass!";
#cat test1.sh不存在,错误输出打印到屏幕,正确输出通过管道发送给grep
 
 
[chengmo@centos5 shell]$ cat test.sh test1.sh 2>/dev/null | grep -n 'echo' 
5:    echo "very good!";
7:    echo "good!";
9:    echo "pass!";
11:    echo "no pass!";
#将test1.sh 没有找到错误输出重定向输出给/dev/null 文件,正确输出通过管道发送给grep
 
 
[chengmo@centos5 shell]$ cat test.sh | ls
catfile      httprequest.txt  secure  test            testfdread.sh  testpipe.sh    testsh.sh      testwhile2.sh
envcron.txt  python           sh      testcase.sh     testfor2.sh    testselect.sh  test.txt       text.txt
env.txt      release          sms     testcronenv.sh  testfor.sh     test.sh        testwhile1.sh
#读取test.sh内容,通过管道发送给ls命令,由于ls 不支持标准输入,因此数据被丢弃

这里实例就是对上面2点注意的验证。作用接收标准输入的命令才可以用作管道右边。否则传递过程中数据会抛弃。 常用来作为接收数据管道命令有:sed,awk,cut,head,top,less,more,wc,join,sort,split 等等,都是些文本处理命令。

  • 管道命令与重定向区别

区别是:

1、左边的命令应该有标准输出 | 右边的命令应该接受标准输入
   左边的命令应该有标准输出 > 右边只能是文件
   左边的命令应该需要标准输入 < 右边只能是文件

2、管道触发两个子进程执行"|"两边的程序;而定向是在一个进程内执行

这些都是网上总结很多的,其实只要多加清楚用法,也一定有自己的一份不同描述。

实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#可以相互转换情况
#输入重定向
 
[chengmo@centos5 shell]$ cat test.sh| grep -n 'echo'
5:    echo "very good!";
7:    echo "good!";
9:    echo "pass!";
11:    echo "no pass!";
#"|"管道两边都必须是shell命令
 
 
[chengmo@centos5 shell]$ grep -n 'echo' <test.sh   
5:    echo "very good!";
7:    echo "good!";
9:    echo "pass!";
11:    echo "no pass!";
#"重定向"符号,右边只能是文件(普通文件,文件描述符,文件设备)
 
 
[chengmo@centos5 shell]$ mail -s 'test' 8292669@qq.com <test.sh
[chengmo@centos5 shell]$ cat test.sh|mail -s 'test' 8292669@qq.com
#以上2个也相同,将test.sh内容发送到指定邮箱。
 
 
[chengmo@centos5 shell]$ (sed -n '1,$p'|grep -n 'echo')<test.sh
5:    echo "very good!";
7:    echo "good!";
9:    echo "pass!";
11:    echo "no pass!";
#这个脚本比较有意思了。由于前面是管道,后面需要把test.sh内容重定向到 sed ,然后sed输出通过管道,输入给grep.需要将前面用"()"运算符括起来。在单括号内的命令,可以把它们看作一个象一个命令样。如果不加括号test.sh就是grep 的输入了。
 
 
#上面一个等同于这个
[chengmo@centos5 shell]$ sed -n '1,$p'<test.sh | grep -n 'echo'
5:    echo "very good!";
7:    echo "good!";
9:    echo "pass!";
11:    echo "no pass!";
 
#重定向运算符,在shell命令解析前,首先检查的(一个命令,执行前一定检查好它的输入,输出,也就是0,1,2 设备是否准备好),所以优先级会最高
 
 
[chengmo@centos5 shell]$ sed -n '1,10p'<test.sh | grep -n 'echo' <testsh.sh
10:echo $total;
18:echo $total;
21:     echo "ok";
#哈哈,这个grep又接受管道输入,又有testsh.sh输入,那是不是2个都接收呢。刚才说了"<"运算符会优先,管道还没有发送数据前,grep绑定了testsh.sh输入,这样sed命令输出就被抛弃了。这里一定要小心使用
 
#输出重定向
 
[chengmo@centos5 shell]$ cat test.sh>test.txt
[chengmo@centos5 shell] cat test.sh|tee test.txt &>/dev/null
#通过管道实现将结果存入文件,还需要借助命令tee,它会把管道过来标准输入写入文件test.txt ,然后将标准输入复制到标准输出(stdout),所以重定向到/dev/null 不显示输出
#">"输出重定向,往往在命令最右边,接收左边命令的,输出结果,重定向到指定文件。也可以用到命令中间。
 
 
[chengmo@centos5 shell]$ ls test.sh test1.sh testsh.sh 2>err.txt | grep 'test'
test.sh
testsh.sh
#目录下面有:test,testsh文件,test1.sh不存在,因此将ls 命令错误输出输入到err.txt 正确输出,还会通过管道发送到grep命令。
[chengmo@centos5 shell]$ ls test.sh test1.sh testsh.sh &>err.txt | grep 'test'
#这次打印结果是空,&代表正确与错误输出 都输入给err.txt,通过管道继续往下面传递数据为空,所以没有什么显示的
 
#同样">"输出重定向符,优先级也是先解析,当一个命令有这个字符,它就会与左边命令标准输出绑定。准备好了这些,就等待命令执行输出数据,它就开始接收

再概括下:

从上面例子可以看,重定向与管道在使用时候很多时候可以通用,其实,在shell里面,经常是【条条大路通罗马】的。一般如果是命令间传递参数,还是管道的好,如果处理输出结果需要重定向到文件,还是用重定向输出比较好。

命令执行顺序可以看下:Linux Shell 通配符、元字符、转义符使用实例介绍

  • shell脚本接收管道输入

有意思的问题:

既然作用管道接收命令,需要可以接收标准的输入,那么我们shell脚本是否可以开发出这样的基本程序呢?(大家经常看到的,都是一些系统的命令作为管道接收方)

实例(testpipe.sh):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/sh
  
 if [ $# -gt 0 ];then
     exec 0<$1;
#判断是否传入参数:文件名,如果传入,将该文件绑定到标准输入
 fi
  
 while read line
 do
     echo $line;
 done<&0;
#通过标准输入循环读取内容
 exec 0&-;
#解除标准输入绑定

运行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[chengmo@centos5 shell]$ cat testpipe.txt
1,t,est pipe
2,t,est pipe
3,t,est pipe
4,t,est pipe
#testpipe.txt 只是需要读取的测试文本
 
[chengmo@centos5 shell]$ cat testpipe.txt | sh testpipe.sh
1,t,est pipe
2,t,est pipe
3,t,est pipe
4,t,est pipe
#通过cat 读取 testpipe.txt 发送给testpipe.sh 标准输入
 
[chengmo@centos5 shell]$ sh testpipe.sh testpipe.txt     
1,t,est pipe
2,t,est pipe
3,t,est pipe
4,t,est pipe
#testpipe.sh 通过出入文件名读取文件内容

linux shell 管道命令(pipe)使用及与shell重定向区别的更多相关文章

  1. Linux的管道命令

    Linux的管道命令 管道命令(Pipe) 管道命令用"|"来表示,管道命令需要接收前一个命令的输出来进行操作,但不能处理前一个命令的错误. //选取界面:cut,grep cut ...

  2. Shell 管道指令pipe

    目录 管道命令pipe 选取命令 cut.grep cut 取出需要的信息 grep 取出需要行.过滤不需要的行 排序命令 sort.wc.uniq sort 排序 假设三位数,按十位数从小到大,个位 ...

  3. 关于Linux_shell中的管道命令pipe “|”的简单学习和使用

    什么是 "|"? |其实是linux shell 中的一个命令:管道命令(pipe) 管道命令操作符是:"|",它仅能处理经由前面一个指令传出的正确输出信息,也 ...

  4. 24 shell 管道命令与过滤器

    1.管道命令的用法 2.使用管道命令的好处: 3.重定向和管道的区别 4.Linux管道实例 5.管道与重定向 1)管道与输入重定向 2)管道与输出重定向 6.过滤器 7.过滤器举栗 1.管道命令的用 ...

  5. linux 之管道命令与重定向

    一.Linux重定向 重定向能够实现Linux命令的输入输出与文件之间重定向,以及实现将多个命令组合起来实现更加强大的命令.这部分涉及到的比较多的命令主要有: 涉及到的比较多的命令主要有: cat:连 ...

  6. Linux用管道命令对文件的移动

    我的问题是这样的:我有一个文件夹,里面有大约有1000个文件,然后我想把这样的一部分文件给随机分成两部分,一部分含有100张,另外一部分含有剩下的所有的文件,这个时候如果是在Linux图形界面的话直接 ...

  7. Linux中常用命令pipe

    大多数linux命令处理数据后都会输出到标准输出,但是如果数据要经过系列列的步骤处理后,才是需要的数据个数,这种需求就需要管道来帮助完成. 管道命令使用"|"作为界定符,将界定符前 ...

  8. Linux系统管道命令符

    管道命令符“|”的作用是将前一个命令的标准输出作为后一个命令的标准输入,格式为“命令A | 命令B” 以下实例中,通过grep命令搜索关键字“/sbin/nologin”在/etc/passwd中查找 ...

  9. linux里面的命令:./和. /(这里有空格)的区别

    /  ( 直接/ ) 直接 / 表示: 根目录. ./ ( 点号紧跟/ ) 点号紧跟/ 表示: 当前目录(相对路径的写法). . / ( 点号 空格 / ) 有空格的点号等同于source命令,表示在 ...

随机推荐

  1. 远程连接Linux相关操作

    1.远程连接的步骤 编译网卡配置文件,修改onboot参数 vi /etc/sysconfig/network-scripts/ifcfg-ens33进入命令之后将onboot改成onboot=yes ...

  2. 【leetcode】1081. Smallest Subsequence of Distinct Characters

    题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...

  3. 【leetcode】877. Stone Game

    题目如下: Alex and Lee play a game with piles of stones.  There are an even number of piles arranged in ...

  4. 对webpack的初步研究2

    Entry Points 如“ 入门”中所述,有多种方法可以entry在webpack配置中定义属性.我们会告诉你,你的方法可以配置的entry属性,除了解释为什么它可能对你有用 Single Ent ...

  5. [原创] Delphi Win API函数 操作帮助文件 HtmlHelpA函数介绍

    Delphi Win API函数 操作帮助文件 HtmlHelpA函数介绍 函数原型:HWND HtmlHelpA( HWND hwndCaller, LPCSTR pszFile, UINT uCo ...

  6. swagger2接口发布demo

    1.目的:使用Swagger2发布接口,ui可操作 2.项目结构  3. 代码 3.1 接口类qinfeng.zheng.api.controller.DemoController package q ...

  7. 用jquery实现图片轮播

    用jquery简单实现图片轮播效果,代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  8. App中h5音频不能播放问题

    前置:以下问题是针对vue项目的解决方案 问题一:IOS中音频不能自动播放 原因:ios禁止了音频自动播放 解决办法:在vue的生命周期mounted中获取音频Dom并调用音频播放方法play(),注 ...

  9. 关于C(n,m) 的奇偶 ,与C(n,0),C(n,1),C(n,2)…C(n,n).当中有多少个奇数

    (n & m) == m  为奇数 C(n,0),C(n,1),C(n,2)…C(n,n).当中有多少个奇数 第一种想法是Lucas定理推导,我们分析一下 C(n,m)%2,那么由lucas定 ...

  10. Android7.1后对于Toast类型窗口的限制

    在处理问题的时候,经常会遇到ANR,停止运行等问题,经过分析和验证,和如下的一行关键log有关 WindowManager: Adding more than one toast window for ...