Linux管道指令(pipe)与shell 重定向的区别

多指令执行python

符号
格式 做用
; comd1;comd2  依次执行
&& comd1 && comd2

comd1正确执行,commd2执行,shell

comm1执行失败, commd2不执行centos

|| comd1 || comd2 

 comm1执行失败,执行commd1less

comm1执行成功,不执行commd2spa

管道 (|)与重定向(>, >>)的区别code

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

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

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

注意:input

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

二、管道命令右边命令,必须可以接收标准输入流命令才行。

实例:

[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 等等,都是些文本处理命令。

  • 管道命令与重定向区别

区别是:

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

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

[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,经过管道继续往下面传递数据为空,因此没有什么显示的
#一样">"输出重定向符,优先级也是先解析,当一个命令有这个字符,它就会与左边命令标准输出绑定。准备好了这些,就等待命令执行输出数据,它就开始接收
相关文章
相关标签/搜索