首页 > 系统 > Linux > 正文

linux shell 管道命令(pipe)使用及与shell重定向区别

2019-10-26 18:47:01
字体:
来源:转载
供稿:网友

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

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

管道命令使用说明:
先看下下面图:

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

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

注意:

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

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

实例:

[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 | lscatfile      httprequest.txt  secure  test            testfdread.sh  testpipe.sh    testsh.sh      testwhile2.shenvcron.txt  python           sh      testcase.sh     testfor2.sh    testselect.sh  test.txt       text.txtenv.txt      release          sms     testcronenv.sh  testfor.sh     test.sh        testwhile1.sh#读取test.sh内容,通过管道发送给ls命令,由于ls 不支持标准输入,因此数据被丢弃            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表