zoukankan      html  css  js  c++  java
  • shell管道和重定向

    看了前面一节:linux shell数据重定向(输入重定向与输出重定向)详细分析 估计还有一些朋友是头晕晕的,好复杂的重定向了。这次我们看下管道命令了。shell管道,可以说用法就简单多了。

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

    一、管道命令使用说明:

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

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

    注意:

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

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

    实例:

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

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

    二、管道命令与重定向区别

    区别是:

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

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

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

    实例:

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

    再概括下:

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

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

    三、shell脚本接收管道输入

     1 有意思的问题:
     2 
     3 既然作用管道接收命令,需要可以接收标准的输入,那么我们shell脚本是否可以开发出这样的基本程序呢?(大家经常看到的,都是一些系统的命令作为管道接收方)
     4 
     5 实例(testpipe.sh):
     6 1
     7 2
     8 3
     9 4
    10 5
    11 6
    12 7
    13 8
    14 9
    15 10
    16 11
    17 12
    18 13
    19 14
    20     
    21 #!/bin/sh
    22   
    23  if [ $# -gt 0 ];then
    24      exec 0<$1;
    25 #判断是否传入参数:文件名,如果传入,将该文件绑定到标准输入
    26  fi
    27   
    28  while read line
    29  do
    30      echo $line;
    31  done<&0;
    32 #通过标准输入循环读取内容
    33  exec 0&-;
    34 #解除标准输入绑定
    35 
    36 运行结果:
    37 1
    38 2
    39 3
    40 4
    41 5
    42 6
    43 7
    44 8
    45 9
    46 10
    47 11
    48 12
    49 13
    50 14
    51 15
    52 16
    53 17
    54 18
    55 19
    56 20
    57     
    58 [chengmo@centos5 shell]$ cat testpipe.txt
    59 1,t,est pipe
    60 2,t,est pipe
    61 3,t,est pipe
    62 4,t,est pipe
    63 #testpipe.txt 只是需要读取的测试文本
    64  
    65 [chengmo@centos5 shell]$ cat testpipe.txt | sh testpipe.sh
    66 1,t,est pipe
    67 2,t,est pipe
    68 3,t,est pipe
    69 4,t,est pipe
    70 #通过cat 读取 testpipe.txt 发送给testpipe.sh 标准输入
    71  
    72 [chengmo@centos5 shell]$ sh testpipe.sh testpipe.txt     
    73 1,t,est pipe
    74 2,t,est pipe
    75 3,t,est pipe
    76 4,t,est pipe
    77 #testpipe.sh 通过出入文件名读取文件内容
  • 相关阅读:
    DGA聚类 使用DBScan
    http://bdsmdvdtube.com/ 罪恶无处不在
    leetcode 202. Happy Number
    广州Uber优步司机奖励政策(1月4日~1月10日)
    武汉ber优步司机奖励政策(1月4日~1月10日)
    厦门Uber优步司机奖励政策(1月4日~1月10日)
    上海Uber优步司机奖励政策(1月4日~1月10日)
    青岛Uber优步司机奖励政策(1月4日~1月10日)
    深圳Uber优步司机奖励政策(1月4日~1月10日)
    成都Uber优步司机奖励政策(1月10日)
  • 原文地址:https://www.cnblogs.com/zmlctt/p/4007441.html
Copyright © 2011-2022 走看看